springboot11-阿里云视频点击

阿里云视频点击

前提条件

1.注册阿里云oss
2.注册阿里云视频点击功能

使用方式

1.视频点击API(推荐使用SDK方式)
2.视频点击SDK

SDK方式注入依赖

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-vod</artifactId>
            <version>2.15.2</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>

初始化

import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;

public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
    
    
	String regionId = "cn-shanghai";  // 点播服务接入区域
	DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
	DefaultAcsClient client = new DefaultAcsClient(profile);
	return client;
}
			

其中使用到OSS的id与秘钥

视频上传

添加依赖

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-sdk-vod-upload</artifactId>
    <version>1.4.11</version>
</dependency>

由于该jar包还未开源需要手动下载到maven仓库中

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

由于上文件大小有限制,需要在application中配置上传文件大小。

# 最大上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB

单位为MB不能错否则报错如下
在这里插入图片描述
上传视频使用流上传
代码

public class VodController {
    
    
    @Autowired
    VodService vodService;
    @PostMapping("/uploadVedio")
    public void testUploadVideo(MultipartFile file) throws IOException {
    
    
        String id = VodUtil.ACCESS_KEY_ID;
        String screct = VodUtil.ACCESS_KEY_SECRET;
        // 本地文件的路径和名称
        String fileName = file.getOriginalFilename();
        // 上传到阿里云的显示标题
        String title = fileName.substring(0,fileName.lastIndexOf("."));
        InputStream inputStream= file.getInputStream();
        String videoId = vodService.testUploadVideo(id,screct,title,fileName,inputStream);
        System.out.println(videoId);
    }

}

public class VodService {
    
    
    public String testUploadVideo(String accessKeyId, String accessKeySecret, String title, String fileName, InputStream inputStream) {
    
    
        UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        String videoId = null;
        if (response.isSuccess()) {
    
    
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            videoId = response.getVideoId();
        } else {
    
     //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            videoId = response.getVideoId();
        }
        return videoId;
    }

上传后返回id,通过id获取播放凭证播放视频。

猜你喜欢

转载自blog.csdn.net/weixin_44172800/article/details/106201940