发布时间:2023-09-21 08:00
1、阿里云账号里有余额;2、进入阿里云开通视频点播功能;3、获取阿里云秘钥(AccessKey管理中);4、目录结构:
#服务端口
server.port=8003
#服务名
spring.application.name=service-vod
#环境设置:dev、test、prod
spring.profiles.active=dev
#阿里云 OSS
#不同的服务器,地址不同
aliyun.oss.file.keyid=你的keyid
aliyun.oss.file.keysecret=你的keysecret
#最大上传单个文件大小 默认:1M
spring.servlet.multipart.max-file-size=1024MB
#最大值上传的数据大小 默认10M
spring.servlet.multipart.max-request-size=1024MB
@Component
public class ConstantVodUtils implements InitializingBean {
@Value("${aliyun.oss.file.keyid}")
private String keyid;
@Value("${aliyun.oss.file.keysecret}")
private String keysecret;
public static String ACCESS_KEY_ID;
public static String ACCESS_KEY_SECRET;
@Override
public void afterPropertiesSet() throws Exception {
ACCESS_KEY_ID = keyid;
ACCESS_KEY_SECRET = keysecret;
}
}
public class InitObject {
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
String regionId = "cn-beijing"; // 点播服务接入区域
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
}
@RestController
@CrossOrigin
@RequestMapping("/eduvod/video")
public class VodController {
@Resource
private VodService vodService;
/**
* @Description: 上传视频到阿里云
* @Author: ZhaoLiangLiang
* @date 2021/12/1 23:32
**/
@PostMapping("/uploadAlyVideo")
public R uploadAlyVideo(MultipartFile file){
//返回上传视频id
String videoId = vodService.uploadVideoAly(file);
return R.ok().data("videoId",videoId);
}
/**
* @Description: 根据视频id删除阿里云视频
* @Author: ZhaoLiangLiang
* @date 2021/12/1 23:32
**/
@DeleteMapping("/removeAlyVideo/{id}")
public R removeAlyVideo(@PathVariable("id") String id){
try {
//初始化对象 引入工具类InitVodCilent
DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
//创建删除视频request对象
DeleteVideoRequest request = new DeleteVideoRequest();
//向request设置视频id
request.setVideoIds(id);
//调用初始化对象的方法实现删除
client.getAcsResponse(request);
return R.ok();
}catch (Exception e){
e.printStackTrace();
throw new GuliException(20001,"删除视频失败");
}
}
}
@Service
public class VodServiceImpl implements VodService {
@Override
public String uploadVideoAly(MultipartFile file) {
try{
//accessKeyId,accessKeySecret
//fileName 上传文件原始名称
//inputStream 上传文件输入流
InputStream inputStream = file.getInputStream();
//title 上传之后显示的名称
String filename = file.getOriginalFilename();//得到上传的文件名
String title = filename.substring(0,filename.lastIndexOf("."));
UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET,title,filename,inputStream);
/* 存储区域(可选) */
request.setStorageLocation("视频点播中视频的存储区域");
/* 点播服务接入点 */
request.setApiRegionId("cn-beijing");
UploadVideoImpl upload = new UploadVideoImpl();
UploadStreamResponse response = upload.uploadStream(request);
System.out.println("RequestId="+response.getRequestId()+"\n");//请求视频点播服务的请求ID
String videoId = null;
if(response.isSuccess()){
videoId = response.getVideoId();
System.out.println("VideoId="+response.getVideoId()+"\n");
}else{//如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回的
videoId = response.getVideoId();
System.out.println("VideoId="+response.getVideoId()+"\n");
System.out.println("ErrorCode="+response.getCode()+"\n");
System.out.println("ErrorMessage="+response.getMessage()+"\n");
}
return videoId;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
<el-form-item label="上传视频">
<el-upload
:on-success="handleVodUploadSuccess"
:on-remove="handleVodRemove"
:before-remove="beforeVodRemove"
:on-exceed="handleUploadExceed"
:file-list="fileList"
:action="BASE_API+'/eduvod/video/uploadAlyVideo'"
:limit="1"
class="upload-demo">
<el-button size="small" type="primary">上传视频el-button>
<el-tooltip placement="right-end">
<div slot="content">最大支持1G,<br>
支持3GP、ASF、AVI、DAT、DV、FLV、F4V、<br>
GIF、M2T、M4V、MJ2、MJPEG、MKV、MOV、MP4、<br>
MPE、MPG、MPEG、MTS、OGG、QT、RM、RMVB、<br>
SWF、TS、VOB、WMV、WEBM 等视频格式上传div>
<i class="el-icon-question"/>
el-tooltip>
el-upload>
el-form-item>
//成功回调
handleVodUploadSuccess(response, file, fileList) {
console.log(response.data);
if(response.data.videoId){
this.flag = true;
}
//上传之后视频id赋值
this.video.videoSourceId = response.data.videoId;
//上传之后视频名称赋值
this.video.videoOriginalName = file.name;
},
//视图上传多于一个视频
handleUploadExceed(files, fileList) {
this.$message.warning('想要重新上传视频,请先删除已上传的视频')
},
//点击确定 调用这个方法
handleVodRemove(){
//调用删除视频的接口
video.deleteAlyVideo(this.video.videoSourceId).then(res=>{
//提示信息
this.$message({
type: "success",
message: "删除视频成功!!!"
})
//文件列表清空
this.fileList = [];
this.video.videoSourceId = "";
this.video.videoOriginalName = "";
})
},
//点击x调用这个方法
beforeVodRemove(file,FileList) {
return this.$confirm(`确定移除${file.name}吗???`);
},
阿里云视频点播上传视频时InvalidStorage.NotFound.
一直是上传中,并且代码中没有明显的报错信息, 只打印出 ErrorCode=null ErrorMessage=sun/misc/BASE64Ecoder.