一、创建视频点播微服务
1、创建微服务模块
Artifact:service-vod
2、pom
(1)service-vod中引入依赖
3、application.properties
# 服务端口
server.port=8003
# 服务名
spring.application.name=service-vod1
# 环境设置:dev、test、prod
spring.profiles.active=dev
#阿里云 vod
#不同的服务器,地址不同
aliyun.vod.file.keyid=LTAI4FyKCNK4Rfc21rEw3UHi
aliyun.vod.file.keysecret=cMFEuiQNmd738Eg7c7qqleOBQoDOKG
# 最大上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB
4、logback.xml
5、启动类
二、整合阿里云vod实现视频上传
1、创建常量类
ConstantPropertiesUtil.java
5、创建controller
@Component public class ConstantPropertiesUtil implements InitializingBean { @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; } }
2、复制工具类到项目中
AliyunVodSDKUtils .java
public class AliyunVodSDKUtils { 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、配置swagger
web和admin
4、创建service
接口:VideoService.java
@Service public interface EduVideoService { public String uploadVideo(MultipartFile file); void removeMoreAlyVideo(List<String> videoIdList); }
实现:VideoServiceImpl.java
@Service public class EduVideoServiceImpl implements EduVideoService { @Override public String uploadVideo(MultipartFile file) { try { String fileName = file.getOriginalFilename(); String title=fileName.substring(0,fileName.lastIndexOf(".")); InputStream inputStream = file.getInputStream(); UploadStreamRequest request = new UploadStreamRequest(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET, title, fileName, inputStream); //request.setStorageLocation("outin-16da2b9dc07711ebbc7500163e10ce6c.oss-cn-beijing.aliyuncs.com"); UploadVideoImpl uploader = new UploadVideoImpl(); UploadStreamResponse response = uploader.uploadStream(request); System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID String videoId=""; if (response.isSuccess()) { System.out.print("VideoId=" + response.getVideoId() + "\n"); videoId=response.getVideoId(); } 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"); videoId=response.getVideoId(); } return videoId; }catch (Exception e) { e.printStackTrace(); return null; } } @Override public void removeMoreAlyVideo(List<String> videoIdList) { String join = StringUtils.join(videoIdList.toArray(), ","); try{ DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET); DeleteVideoRequest request = new DeleteVideoRequest(); request.setVideoIds(join); client.getAcsResponse(request); } catch (Exception e){ e.printStackTrace(); throw new WangException(20001,"删除失败"); } } }
5、创建controller
VideoAdminController.java
@RestController @RequestMapping("/vod/video") //@CrossOrigin public class VideoController { @Autowired private EduVideoService eduVideoService; @PostMapping("upload") public R uploadVideo(MultipartFile file){ String videoId= eduVideoService.uploadVideo(file); return R.ok().data("videoId",videoId); } @DeleteMapping("delete/{id}") public R removeALYVideo(@PathVariable String id){ try{ DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET); DeleteVideoRequest request = new DeleteVideoRequest(); request.setVideoIds(id); client.getAcsResponse(request); return R.ok(); } catch (Exception e){ e.printStackTrace(); throw new WangException(20001,"删除失败"); } } //删除多个阿里云视频的方法 //参数多个视频id List videoIdList @DeleteMapping("delete-batch") public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) { eduVideoService.removeMoreAlyVideo(videoIdList); return R.ok(); } @GetMapping("getVideoPlayAuth/{videoId}") public R getVideoPlayAuth(@PathVariable String videoId){ try{ String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID; String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET; DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret); GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest(); request.setVideoId(videoId); GetVideoPlayAuthResponse response = client.getAcsResponse(request); String playAuth = response.getPlayAuth(); return R.ok().data("playAuth",playAuth); } catch (Exception e) { e.printStackTrace(); return null; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律