2)初始化操作,创建 DefaultAcsClient 对象
public class InitVodClient { 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; } }
(3)实现根据视频id获取视频播放地址
//根据id获取视频播放地址 public static void getPlayUrl()throws ClientException{ //创建初始化对象 DefaultAcsClient client = InitVodClient.initVodClient("your keyId", "your keySecret"); //创建获取视频地址request和response GetPlayInfoRequest request = new GetPlayInfoRequest(); GetPlayInfoResponse response = new GetPlayInfoResponse(); request.setVideoId("efc88346e5dd45fda5b161c9ddbd0d9d"); //调用初始化对象里面的方法,传递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"); }
(4)获取视频播放凭证
//根据id获取视频播放凭证 public static void getPlayAuth()throws ClientException{ //创建初始化对象 DefaultAcsClient client = InitVodClient.initVodClient("your keyId", "your keySecret"); GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest(); GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse(); request.setVideoId("003a83891a7b444d93727d83a5ba7d3a"); response = client.getAcsResponse(request); System.out.println("palyAuth:"+response.getPlayAuth()); }
5)上传视频到阿里云视频点播服务
//以文件流的方式进行上传 public static void testUploadFileStream(){ String accessKeyId = "your keyId"; String accessKeySecret = "your keySecret"; String title = "6 - What If I Want to Move Faster123.mp4 "; String fileName = "F:/自学课程/项目资料/02谷粒学苑(分布式项目)/项目资料/1-阿里云上传测试视频/6 - What If I Want to Move Faster.mp4"; UploadFileStreamRequest request = new UploadFileStreamRequest(accessKeyId, accessKeySecret, title, fileName); UploadVideoImpl uploader = new UploadVideoImpl(); UploadFileStreamResponse response = uploader.uploadFileStream(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"); } }
七、添加小节实现视频上传和删除 – 后端
1、引入依赖
2、创建application配置文件
#服务端口 server: port: 8003 #服务名 spring: application: name: service-vod #环境设置:dev,test,prod profiles: active: dev servlet: multipart: max-file-size: 1024MB # 最大上传单个文件大小:默认1M max-request-size: 1024MB # 最大置总上传的数据大小 :默认10M #阿里云VOD地址 aliyun: vod: file: keyid: your keyId keysecret: your keySecret
3、常量工具类
@Component //把属性的设置交给Spring. public class ConstantPropertiesUtil implements InitializingBean {// InitializingBean:在初始化的时候,该类被执行. //从配置文件中读取值,赋值给这些属性 //注意@value无法给静态属性注入值 @Value("${aliyun.vod.file.keyid}") private String keyId; @Value("${aliyun.vod.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; } }
4、主启动类
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @ComponentScan("com.rg")//为了扫描外部的swagger public class VodApplication { public static void main(String[] args) { SpringApplication.run(VodApplication.class, args); } }
5、编写Controller类
@RestController @RequestMapping("/vodService/video") @CrossOrigin public class VodController { @Autowired private VodService vodService; //上传视频到阿里云VOD @PostMapping("uploadVideoAly") public R uploadVideoAly(@RequestBody MultipartFile file){ String videoId = vodService.uploadVideoAly(file); return R.ok().data("videoId",videoId); } //根据视频id从阿里云删除视频 @DeleteMapping("removeAlyVideo/{id}") public R removeAlyVideo(@PathVariable String id){ vodService.removeAlyVideo(id); return R.ok(); } }
6、编写Service类
@Service public class VodServiceImpl implements VodService { //上传视频到阿里云VOD @Override public String uploadVideoAly(MultipartFile file) { String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID; String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET; String title = file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf("."));//上传之后显示名称 String fileName = file.getOriginalFilename();//上传文件原始名称 try { InputStream inputStream = file.getInputStream();//上传文件流 UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream); UploadVideoImpl uploader = new UploadVideoImpl(); UploadStreamResponse response = uploader.uploadStream(request); String videoId = response.getVideoId(); return videoId; } catch (Exception e) { e.printStackTrace(); throw new GuLiException(20001, "文件上传失败!"); } } //根据id删除阿里云上的视频 @Override public void removeAlyVideo(String id) { try { DefaultAcsClient client = InitVodClient.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET); // DeleteVideoRequest request = new DeleteVideoRequest(); DeleteVideoResponse response = new DeleteVideoResponse(); //想request中设置视频id request.setVideoIds(id); response = client.getAcsResponse(request); } catch (ClientException e) { e.printStackTrace(); throw new GuLiException(20001, "视频删除失败!"); } } }
八、添加小节实现视频上传和删除 – 前端
1、定义api
//根据id删除视频 removeAlyVideo(id) { return request({ url: `/vodService/video/removeAlyVideo/${id}`, method: 'delete' }) }
2、整合上传组件
<el-form-item label="上传视频"> <el-upload :on-success="handleVodUploadSuccess" :on-remove="handleVodRemove" :before-remove="beforeVodRemove" :on-exceed="handleUploadExceed" :file-list="fileList" :action="BASE_API+'/vodService/video/uploadVideoAly'" :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>
3、数据定义
fileList: [],//上传文件列表 BASE_API: process.env.BASE_API // 接口API地址 video: {// 课时对象 title: '', sort: 0, isFree: 0, videoSourceId: '', videoOriginalName:'' }
4、页面js方法
//上传视频超过最大数量的方法 handleUploadExceed(file,fileList){ this.$message.warning('想要重新上传视频,请先删除已经上传的视频') }, //点击确定调用的方法 handleVodRemove(){ video.removeAlyVideo(this.video.videoSourceId).then(response=>{ this.$message({ type:'success', message:'删除视频成功!' }) //把文件列表清空 this.fileList = [] //删除的视频信息不再存入数据库 this.video.videoSourceId = '' this.video.videoOriginalName = '' }) }, //点击×调用的方法 beforeVodRemove(file,fileList){ return this.$confirm(`确定移除 ${file.name}?`) }, //上传成功方法 handleVodUploadSuccess(response,file,fileList){ this.video.videoSourceId = response.data.videoId this.video.videoOriginalName = file.name }
5、编辑小节时视频回显
6、页面效果展示
如果有收获!!! 希望老铁们来个三连,点赞、收藏、转发。
创作不易,别忘点个赞,可以让更多的人看到这篇文章,顺便鼓励我写出更好的博客