阿里云视频点播服务上传视频

先完成阿里云官方的前置任务
在这里插入图片描述
确保你的RAM访问控制中的用户权限,四个vod 的权限
在这里插入图片描述

添加依赖

<dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.5.1</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
        <version>3.10.2</version>
    </dependency>
     <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-vod</artifactId>
        <version>2.15.11</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.28</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20170516</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-vod-upload</artifactId>
        <version>1.4.14</version>
    </dependency>

特别注意这个依赖
在这里插入图片描述
可以先把这个SDK下载解压,然后在它的lib目录下打开cmd,执行命令:

mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-java-vod-upload -Dversion=1.4.14 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.14.jar

成功页面
在这里插入图片描述

service层代码:

@Service
public class VodServiceImpl implements VodService {

    @Autowired
    VodProperties vodProperties;

    @Override
    public String uploadVideo(MultipartFile file) {
        UploadStreamRequest request = null;
        try{
            request = new UploadStreamRequest(vodProperties.getAccessKeyId(),
                    vodProperties.getAccessKeySecret(),
                    file.getOriginalFilename(),
                    file.getOriginalFilename(),
                    file.getInputStream());
        }catch (Exception e){
            throw new BusinessException();
        }
        UploadVideoImpl uploader = new UploadVideoImpl();   // 客户端对象
        UploadStreamResponse response = uploader.uploadStream(request); // 客户端发起请求
        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        if (response.isSuccess()) {
            System.out.print("VideoId=" + response.getVideoId() + "\n");
        } else {
            /* 如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 */
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }
        return response.getVideoId();
    }
}

VodProperties文件:

@Data
@Component
@ConfigurationProperties(prefix = "aliyun.vod")
public class VodProperties {
    String accessKeyId;
    String accessKeySecret;
    String workflowId; 
    String regionId;  
}

application.yml文件

aliyun:
  vod:
    accessKeyId: "你的accessKeyId"
    accessKeySecret: "你的accessKeySecret"
    workflowId: "你的workflowId"
    regionId: "cn-shanghai"

controller层

// 视频上传
    @PostMapping("uploadVideo")
    public R uploadVideo(MultipartFile file){
        String string = vodService.uploadVideo(file);
        return R.ok(string);
    }

测试成功
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值