Java代码实现阿里云视频上传
Posted 渔光一点萤@
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java代码实现阿里云视频上传相关的知识,希望对你有一定的参考价值。
目录
视频上传
新建一个springboot项目,结构大概这样
添加依赖
<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>
创建service层
package com.cjw.video.service;
import org.springframework.web.multipart.MultipartFile;
public interface VideoService
void uploadVideo(MultipartFile file);
package com.cjw.video.service.impl;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.cjw.video.service.VideoService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
@Service
public class VideoServiceImpl implements VideoService
@Override
public void uploadVideo(MultipartFile file)
try
String originalFilename = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
testUploadStream("LTAIrNZcZiRfgSys","n9TuOpdaVjl4sZkZusepek","aa",originalFilename,inputStream);
catch (Exception e)
e.printStackTrace();
/**
* 流式上传接口
*
* @param accessKeyId
* @param accessKeySecret
* @param title
* @param fileName
* @param inputStream
*/
private static void testUploadStream(String accessKeyId, String accessKeySecret, String title, String fileName, InputStream inputStream)
UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
/* 是否使用默认水印(可选),指定模板组ID时,根据模板组配置确定是否使用默认水印*/
//request.setShowWaterMark(true);
/* 自定义消息回调设置,参数说明参考文档 https://help.aliyun.com/document_detail/86952.html#UserData */
//request.setUserData(""\\"Extend\\":\\"test\\":\\"www\\",\\"localId\\":\\"xxxx\\",\\"MessageCallback\\":\\"CallbackURL\\":\\"http://test.test.com\\""");
/* 视频分类ID(可选) */
//request.setCateId(0);
/* 视频标签,多个用逗号分隔(可选) */
//request.setTags("标签1,标签2");
/* 视频描述(可选) */
//request.setDescription("视频描述");
/* 封面图片(可选) */
//request.setCoverURL("http://cover.sample.com/sample.jpg");
/* 模板组ID(可选) */
//request.setTemplateGroupId("8c4792cbc8694e7084fd5330e56a33d");
/* 工作流ID(可选) */
//request.setWorkflowId("d4430d07361f0*be1339577859b0177b");
/* 存储区域(可选) */
//request.setStorageLocation("in-201703232118266-5sejdln9o.oss-cn-shanghai.aliyuncs.com");
/* 开启默认上传进度回调 */
// request.setPrintProgress(true);
/* 设置自定义上传进度回调 (必须继承 VoDProgressListener) */
// request.setProgressListener(new PutObjectProgressListener());
/* 设置应用ID*/
//request.setAppId("app-1000000");
/* 点播服务接入点 */
//request.setApiRegionId("cn-shanghai");
/* ECS部署区域*/
// request.setEcsRegionId("cn-shanghai");
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");
复制过去会发现编译器爆红,有几个包导不了,需要去阿里云下载
下载好了解压,
只需要这一个jar包
然后去idea找到File --> project Structure --> modules
选择刚刚下载的 aliyun-java-vod-upload-1.4.14.jar ,应用,然后就可以导包了。
对于这个 testUploadStream(“LTAI4G7nRizWrNZcZ”,“n9TuOVjl4sZkZusepek”,“aa”,originalFilename,inputStream);
第一个参数accessKeyId,第二个accessKeySecret,前两个在自己阿里云里面可以找到(如果没找到,可以看我之前的一篇文章,https://blog.csdn.net/weixin_46146197/article/details/121793267?spm=1001.2014.3001.5502)里面有教程。
第三个title是表示存储在阿里云的名字(这里我是写死了aaa所以每个上传好的视频都叫aaa,你可以获取好原视频的名称再放进去String substring = originalFilename.substring(0, originalFilename.lastIndexOf("."));)。
第四个是原文件名字。
第五个是上传文件输入流。
再创建controller
package com.cjw.video.controller;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.cjw.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
@RestController
@RequestMapping("/video")
public class videoController
@Autowired
private VideoService videoService;
@PostMapping(value = "uploadVideo/interAspect",headers = "content-type=multipart/form-data")
public void uploadVideo(@RequestParam("file") MultipartFile file) throws IOException
videoService.uploadVideo(file);
最后配置文件
#服务端口
server.port=8999
#服务名
spring.application.name=uploadVideo
#环境设置:
spring.profiles.active=dev
# 最大上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB
logging.level.root = INFO
用postman测试
用postman测试的时候,接口需要再最后加interAspect(我上面代码就加了),不然通不了
上传成功返回一个videoId,然后把这个id存到数据库就可。
视频删除
controller
@DeleteMapping(value = "deleteUploadVideo/interAspect/id")
public void deleteUploadVideo(@PathVariable("id") String id)
videoService.deleteUploadVideo(id);
serviceImpl
@Override
public void deleteUploadVideo(String id)
try
//初始化对象
DefaultAcsClient client = getClient("LTAI4G7nRizWcZiRfgSys","n9TuOpZPW9QNyusepek");
//创建删除视频request对象
DeleteVideoRequest request = new DeleteVideoRequest();
//向request设置视频id
request.setVideoIds(id);
//调用初始化对象的方法实现删除
client.getAcsResponse(request);
catch(Exception e)
e.printStackTrace();
public static DefaultAcsClient getClient(String accessKeyId, String accessKeySecret) throws ClientException
String regionId = "cn-shanghai"; // 点播服务接入区域
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
Java使用阿里云视频点播
阿里云视频点播服务
视频点播 是集音视频采集、编辑、上传、自动化转码处理、媒体资源管理、分发加速于一体的一站式音视频点播方案。
登录阿里云 搜索 视频点播 我们的账号需要实名认证 且功能是需要付费了
点击开通 我们选择按量付费
来到控制台
可以点击上传视频
点击存储管理
这里没开启是要点击开启的不然无法使用 使用的是oos存储的视频
我们这里进行上传视频测试
我们使用java的SDK操作视频点播
首先引入所需的依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.1</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.62</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-kms</artifactId>
<version>2.10.1</version>
</dependency>
<repositories>
<repository>
<id>sonatype-nexus-staging</id>
<name>Sonatype Nexus Staging</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
创建一个初始化客户端
注意这里的 密钥是 阿里云个人中心开启的
/**
* @create: 2021/10/31
* @author: Tony Stark
*/
public class InitObject {
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;
}
}
视频的播放 获取视频url
/**
* 根据视频id获取视频地址
* @create: 2021/10/31
* @author: Tony Stark
*/
public class TestVod {
public static void main(String[] args) throws ClientException {
//根据视频id获取视频播放地址
//创建初始化对象 设置密钥
DefaultAcsClient client = InitObject.initVodClient("你的accessKeyId", "你的accessKeySecret");
//创建获取视频地址热request和response
GetPlayInfoRequest request = new GetPlayInfoRequest();
GetPlayInfoResponse response = new GetPlayInfoResponse();
//向request中设置视频的id值
request.setVideoId("e921235475d0428eafc4a0cfa819a777");
//调用初始化对象里面的方法传递request对象,获取数据
response=client.getAcsResponse(request);
List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
//播放地址
for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\\n");
}
//Base信息
System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\\n");
}
}
获取视频播放凭证
我们在视频点播中上传视频是可以用魔板加密的 视频加密后需要获取凭证才能播放
public static void getAuth() throws Exception{
//创建初始化对象
DefaultAcsClient client = InitObject.initVodClient("你的accessKeyId", "你的accessKeySecret");
//创建获取视频凭证的的request和response
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
//设置视频id
request.setVideoId("e921235475d0428eafc4a0cfa819a777");
//调用初始化对象里面的方法传递request对象,获取数据
response=client.getAcsResponse(request);
//获取视频凭证
System.out.println(response.getPlayAuth());
}
删除视频
public static DeleteVideoResponse deleteVideo()throws Exception{
//创建初始化对象
DefaultAcsClient client = InitObject.initVodClient("你的accessKeyId", "你的accessKeySecret");
DeleteVideoRequest request = new DeleteVideoRequest();
//支持传入多个视频ID,多个用逗号分隔
request.setVideoIds("VideoId1,VideoId2");
return client.getAcsResponse(request);
}
以上是关于Java代码实现阿里云视频上传的主要内容,如果未能解决你的问题,请参考以下文章