阿里云视频点播服务
一、創(chuàng)建視頻點(diǎn)播微服務(wù)
1、創(chuàng)建微服務(wù)模塊
Artifact:service-vod
(1)service-vod中引入依賴
<dependencies><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId></dependency><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-vod</artifactId></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-sdk-vod-upload</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>org.json</groupId><artifactId>json</artifactId></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId></dependency><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId></dependency> </dependencies>3、application.properties
# 服務(wù)端口 server.port=8003 # 服務(wù)名 spring.application.name=service-vod# 環(huán)境設(shè)置:dev、test、prod spring.profiles.active=dev#阿里云 vod #不同的服務(wù)器,地址不同 aliyun.vod.file.keyid=your accessKeyId aliyun.vod.file.keysecret=your accessKeySecret# 最大上傳單個(gè)文件大小:默認(rèn)1M spring.servlet.multipart.max-file-size=1024MB # 最大置總上傳的數(shù)據(jù)大小 :默認(rèn)10M spring.servlet.multipart.max-request-size=1024MB4、logback.xml
5、啟動(dòng)類
package com.south.vod;@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @ComponentScan(basePackages={"com.south"}) public class VodApplication {public static void main(String[] args) {SpringApplication.run(VodApplication.class, args);} }二、整合阿里云vod實(shí)現(xiàn)視頻上傳
1、創(chuàng)建常量類
ConstantPropertiesUtil.java
package com.south.vod.util;@Component //@PropertySource("classpath:application.properties") 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;@Overridepublic void afterPropertiesSet() throws Exception {ACCESS_KEY_ID = keyId;ACCESS_KEY_SECRET = keySecret;} }2、復(fù)制工具類到項(xiàng)目中
AliyunVodSDKUtils.java
package com.south.vod.util;public class AliyunVodSDKUtils {public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {String regionId = "cn-shanghai"; // 點(diǎn)播服務(wù)接入?yún)^(qū)域DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);DefaultAcsClient client = new DefaultAcsClient(profile);return client;} }3、配置swagger
web和admin
4、創(chuàng)建service
接口:VideoService.java
package com.guli.vod.service; public interface VideoService {String uploadVideo(MultipartFile file); }實(shí)現(xiàn):VideoServiceImpl.java
package com.south.vod.service.impl;@Service public class VideoServiceImpl implements VideoService {@Overridepublic String uploadVideo(MultipartFile file) {try {InputStream inputStream = file.getInputStream();String originalFilename = file.getOriginalFilename();String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));UploadStreamRequest request = new UploadStreamRequest(ConstantPropertiesUtil.ACCESS_KEY_ID,ConstantPropertiesUtil.ACCESS_KEY_SECRET,title, originalFilename, inputStream);UploadVideoImpl uploader = new UploadVideoImpl();UploadStreamResponse response = uploader.uploadStream(request);//如果設(shè)置回調(diào)URL無效,不影響視頻上傳,可以返回VideoId同時(shí)會(huì)返回錯(cuò)誤碼。// 其他情況上傳失敗時(shí),VideoId為空,此時(shí)需要根據(jù)返回錯(cuò)誤碼分析具體錯(cuò)誤原因String videoId = response.getVideoId();if (!response.isSuccess()) {String errorMessage = "阿里云上傳錯(cuò)誤:" + "code:" + response.getCode() + ", message:" + response.getMessage();log.warn(errorMessage);if(StringUtils.isEmpty(videoId)){throw new SouthException(20001, errorMessage);}}return videoId;} catch (IOException e) {throw new SouthException(20001, "guli vod 服務(wù)上傳失敗");}} }5、創(chuàng)建controller
VideoAdminController.java
package com.south.vod.controller.admin;@Api(description="阿里云視頻點(diǎn)播微服務(wù)") @CrossOrigin //跨域 @RestController @RequestMapping("/admin/vod/video") public class VideoAdminController {@Autowiredprivate VideoService videoService;@PostMapping("upload")public R uploadVideo(@ApiParam(name = "file", value = "文件", required = true)@RequestParam("file") MultipartFile file) throws Exception {String videoId = videoService.uploadVideo(file);return R.ok().message("視頻上傳成功").data("videoId", videoId);} }總結(jié)
- 上一篇: 家电行业售后服务管理系统有哪些黑科技应用
- 下一篇: 微信sdk相关配置