首先查看官方文档(重点是服务端SDK)
1. 获取阿里云视频点播地址未加密
1.1 创建阿里云视频点播初始化对象
public class InitObject {
//填入AccessKey信息
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;
}
}
1.2 创建对象和获取阿里云视频点播地址
public class GetPlayInfoById {
/*获取播放地址函数*/
public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client) throws Exception {
//request创建
GetPlayInfoRequest request = new GetPlayInfoRequest();
// todo 传入阿里云视频id
request.setVideoId("");
return client.getAcsResponse(request);
}
/*以下为调用示例*/
public static void main(String[] argv) throws Exception{
//1.创建初始化对象
// todo 传入我们自己的阿里云oos id和密钥
DefaultAcsClient client = InitObject.initVodClient("","");
//2.创建response和request
//然后可根据response来获取未加密的地址
GetPlayInfoResponse response = new GetPlayInfoResponse();
try {
response = getPlayInfo(client);
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");
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
}
}
1.2. 获取阿里云视频播放凭证
public class GetPlayInfoByIdSegret {
/*获取播放凭证函数*/
public static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client) throws Exception {
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
// todo 传入阿里云视频id
request.setVideoId("");
return client.getAcsResponse(request);
}
/*以下为调用示例*/
public static void main(String[] argv) throws Exception{
// todo 传入我们自己的阿里云oos id和密钥
DefaultAcsClient client = InitObject.initVodClient("","");
GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
try {
response = getVideoPlayAuth(client);
//播放凭证
System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
//VideoMeta信息
System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
}
}
2.本地视频上传
2.1 在官网引入jar包
2.2 按照官网指示在idea中导入jar包
2.3 使用官网的示例代码
public class UploadVideoDemo {
//todo 账号AK信息请填写(必选)
private static final String accessKeyId = "";
//todo 账号AK信息请填写(必选)
private static final String accessKeySecret = "";
public static void main(String[] args) {
// 一、视频文件上传
// 视频标题(必选)
String title = "测试标题";
// 1.本地文件上传和文件流上传时,文件名称为上传文件绝对路径,如:/User/sample/文件名称.mp4 (必选)
// 2.网络流上传时,文件名称为源文件名,如文件名称.mp4(必选)。
// 3.流式上传时,文件名称为源文件名,如文件名称.mp4(必选)。
// 任何上传方式文件名必须包含扩展名
String fileName = "";
// 本地文件上传
testUploadVideo(accessKeyId, accessKeySecret, title, fileName);
}
/**
* 本地文件上传接口
*
* @param accessKeyId
* @param accessKeySecret
* @param title
* @param fileName
*/
private static void testUploadVideo(String accessKeyId, String accessKeySecret, String title, String fileName) {
UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
/* 可指定分片上传时每个分片的大小,默认为2M字节 */
request.setPartSize(2 * 1024 * 1024L);
/* 可指定分片上传时的并发线程数,默认为1,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/
request.setTaskNum(1);
/* 是否开启断点续传, 默认断点续传功能关闭。当网络不稳定或者程序崩溃时,再次发起相同上传请求,可以继续未完成的上传任务,适用于超时3000秒仍不能上传完成的大文件。
注意: 断点续传开启后,会在上传过程中将上传位置写入本地磁盘文件,影响文件上传速度,请您根据实际情况选择是否开启*/
//request.setEnableCheckpoint(false);
/* OSS慢请求日志打印超时时间,是指每个分片上传时间超过该阈值时会打印debug日志,如果想屏蔽此日志,请调整该阈值。单位: 毫秒,默认为300000毫秒*/
//request.setSlowRequestsThreshold(300000L);
/* 可指定每个分片慢请求时打印日志的时间阈值,默认为300s*/
//request.setSlowRequestsThreshold(300000L);
/* 是否显示水印(可选),指定模板组ID时,根据模板组配置确定是否显示水印*/
//request.setIsShowWaterMark(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(false);
/* 设置自定义上传进度回调 (必须继承 VoDProgressListener) */
//request.setProgressListener(new PutObjectProgressListener());
/* 设置您实现的生成STS信息的接口实现类*/
// request.setVoDRefreshSTSTokenListener(new RefreshSTSTokenImpl());
/* 设置应用ID*/
//request.setAppId("app-1000000");
/* 点播服务接入点 */
//request.setApiRegionId("cn-shanghai");
/* ECS部署区域*/
// request.setEcsRegionId("cn-shanghai");
UploadVideoImpl uploader = new UploadVideoImpl();
UploadVideoResponse response = uploader.uploadVideo(request);
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID
if (response.isSuccess()) {
System.out.print("VideoId=" + response.getVideoId() + "\n");
System.out.println("上传视频成功");
} 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");
System.out.println("上传视频失败");
}
}
}
3.用代码在阿里云视频删除代码
根据并结合自己所实现的项目
@DeleteMapping("deleteAliyunVideo/{id}")
public R deleteAliyunVideo(@PathVariable String id){
try {
//1.创建初始化对象
DefaultAcsClient defaultAcsClient = InitVodClient.initVodClient(AliyunVideoConstont.ACCESS_KEY_ID,AliyunVideoConstont.ACCESS_KEY_SECRET);
//2.创建request
DeleteVideoRequest request = new DeleteVideoRequest();
//3.创建request设置视频id
request.setVideoIds(id);
//4.创建response
//5.删除
defaultAcsClient.getAcsResponse(request);
return R.ok();
} catch (ClientException e) {
e.printStackTrace();
throw new MyException(20001,"删除失败");
}
}