图片.png图片.png一、创建视频点播微服务

1、创建微服务模块

Artifact:service-vod

2、pom

(1)service-vod中引入依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-core</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun.oss</groupId>
  8. <artifactId>aliyun-sdk-oss</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>com.aliyun</groupId>
  12. <artifactId>aliyun-java-sdk-vod</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>com.aliyun</groupId>
  16. <artifactId>aliyun-sdk-vod-upload</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.alibaba</groupId>
  20. <artifactId>fastjson</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.json</groupId>
  24. <artifactId>json</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>com.google.code.gson</groupId>
  28. <artifactId>gson</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>joda-time</groupId>
  32. <artifactId>joda-time</artifactId>
  33. </dependency>
  34. </dependencies>

3、application.properties

  1. # 服务端口
  2. server.port=8003
  3. # 服务名
  4. spring.application.name=service-vod
  5. # 环境设置:devtestprod
  6. spring.profiles.active=dev
  7. #阿里云 vod
  8. #不同的服务器,地址不同
  9. aliyun.vod.file.keyid=your accessKeyId
  10. aliyun.vod.file.keysecret=your accessKeySecret
  11. # 最大上传单个文件大小:默认1M
  12. spring.servlet.multipart.max-file-size=1024MB
  13. # 最大置总上传的数据大小 :默认10M
  14. spring.servlet.multipart.max-request-size=1024MB

4、logback.xml

5、启动类

VodApplication.java

  1. package com.guli.vod;
  2. @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
  3. @ComponentScan(basePackages={"com.atguigu"})
  4. public class VodApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(VodApplication.class, args);
  7. }
  8. }

二、整合阿里云vod实现视频上传

1、创建常量类

ConstantPropertiesUtil.java

  1. package com.guli.vod.util;
  2. @Component
  3. //@PropertySource("classpath:application.properties")
  4. public class ConstantPropertiesUtil implements InitializingBean {
  5. @Value("${aliyun.vod.file.keyid}")
  6. private String keyId;
  7. @Value("${aliyun.vod.file.keysecret}")
  8. private String keySecret;
  9. public static String ACCESS_KEY_ID;
  10. public static String ACCESS_KEY_SECRET;
  11. @Override
  12. public void afterPropertiesSet() throws Exception {
  13. ACCESS_KEY_ID = keyId;
  14. ACCESS_KEY_SECRET = keySecret;
  15. }
  16. }

2、复制工具类到项目中

AliyunVodSDKUtils.java

  1. package com.guli.vod.util;
  2. public class AliyunVodSDKUtils {
  3. public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
  4. String regionId = "cn-shanghai"; // 点播服务接入区域
  5. DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
  6. DefaultAcsClient client = new DefaultAcsClient(profile);
  7. return client;
  8. }
  9. }

3、配置swagger

web和admin

4、创建service

接口:VideoService.java

  1. package com.guli.vod.service;
  2. public interface VideoService {
  3. String uploadVideo(MultipartFile file);
  4. }

实现:VideoServiceImpl.java

  1. package com.guli.vod.service.impl;
  2. @Service
  3. public class VideoServiceImpl implements VideoService {
  4. @Override
  5. public String uploadVideo(MultipartFile file) {
  6. try {
  7. InputStream inputStream = file.getInputStream();
  8. String originalFilename = file.getOriginalFilename();
  9. String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
  10. UploadStreamRequest request = new UploadStreamRequest(
  11. ConstantPropertiesUtil.ACCESS_KEY_ID,
  12. ConstantPropertiesUtil.ACCESS_KEY_SECRET,
  13. title, originalFilename, inputStream);
  14. UploadVideoImpl uploader = new UploadVideoImpl();
  15. UploadStreamResponse response = uploader.uploadStream(request);
  16. //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。
  17. // 其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
  18. String videoId = response.getVideoId();
  19. if (!response.isSuccess()) {
  20. String errorMessage = "阿里云上传错误:" + "code:" + response.getCode() + ", message:" + response.getMessage();
  21. log.warn(errorMessage);
  22. if(StringUtils.isEmpty(videoId)){
  23. throw new GuliException(20001, errorMessage);
  24. }
  25. }
  26. return videoId;
  27. } catch (IOException e) {
  28. throw new GuliException(20001, "guli vod 服务上传失败");
  29. }
  30. }
  31. }

5、创建controller

VideoAdminController.java

  1. package com.guli.vod.service.impl;
  2. @Service
  3. public class VideoServiceImpl implements VideoService {
  4. @Override
  5. public String uploadVideo(MultipartFile file) {
  6. try {
  7. InputStream inputStream = file.getInputStream();
  8. String originalFilename = file.getOriginalFilename();
  9. String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
  10. UploadStreamRequest request = new UploadStreamRequest(
  11. ConstantPropertiesUtil.ACCESS_KEY_ID,
  12. ConstantPropertiesUtil.ACCESS_KEY_SECRET,
  13. title, originalFilename, inputStream);
  14. UploadVideoImpl uploader = new UploadVideoImpl();
  15. UploadStreamResponse response = uploader.uploadStream(request);
  16. //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。
  17. // 其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
  18. String videoId = response.getVideoId();
  19. if (!response.isSuccess()) {
  20. String errorMessage = "阿里云上传错误:" + "code:" + response.getCode() + ", message:" + response.getMessage();
  21. log.warn(errorMessage);
  22. if(StringUtils.isEmpty(videoId)){
  23. throw new GuliException(20001, errorMessage);
  24. }
  25. }
  26. return videoId;
  27. } catch (IOException e) {
  28. throw new GuliException(20001, "guli vod 服务上传失败");
  29. }
  30. }
  31. }

6、启动后端vod微服务

7、swagger测试

三、删除云端视频后端实现

文档:服务端SDK->Java SDK->媒资管理
https://help.aliyun.com/document_detail/61065.html?spm=a2c4g.11186623.6.831.654b3815cIxvma#h2—div-id-deletevideo-div-7

1、service

接口

  1. void removeVideo(String videoId);

实现

  1. @Override
  2. public void removeVideo(String videoId) {
  3. try{
  4. DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
  5. ConstantPropertiesUtil.ACCESS_KEY_ID,
  6. ConstantPropertiesUtil.ACCESS_KEY_SECRET);
  7. DeleteVideoRequest request = new DeleteVideoRequest();
  8. request.setVideoIds(videoId);
  9. DeleteVideoResponse response = client.getAcsResponse(request);
  10. System.out.print("RequestId = " + response.getRequestId() + "\n");
  11. }catch (ClientException e){
  12. throw new GuliException(20001, "视频删除失败");
  13. }
  14. }

2、controller

  1. @DeleteMapping("{videoId}")
  2. public R removeVideo(@ApiParam(name = "videoId", value = "云端视频id", required = true)
  3. @PathVariable String videoId){
  4. videoService.removeVideo(videoId);
  5. return R.ok().message("视频删除成功");
  6. }

二、前端

1、定义api

api/edu/vod.js

  1. import request from '@/utils/request'
  2. const api_name = '/admin/vod/video'
  3. export default {
  4. removeById(id) {
  5. return request({
  6. url: `${api_name}/${id}`,
  7. method: 'delete'
  8. })
  9. }
  10. }