阿里云视频点播服务
一、創建視頻點播微服務
1、創建微服務模塊
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
# 服務端口 server.port=8003 # 服務名 spring.application.name=service-vod# 環境設置:dev、test、prod spring.profiles.active=dev#阿里云 vod #不同的服務器,地址不同 aliyun.vod.file.keyid=your accessKeyId aliyun.vod.file.keysecret=your accessKeySecret# 最大上傳單個文件大小:默認1M spring.servlet.multipart.max-file-size=1024MB # 最大置總上傳的數據大小 :默認10M spring.servlet.multipart.max-request-size=1024MB4、logback.xml
5、啟動類
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實現視頻上傳
1、創建常量類
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、復制工具類到項目中
AliyunVodSDKUtils.java
package com.south.vod.util;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
package com.guli.vod.service; public interface VideoService {String uploadVideo(MultipartFile file); }實現: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);//如果設置回調URL無效,不影響視頻上傳,可以返回VideoId同時會返回錯誤碼。// 其他情況上傳失敗時,VideoId為空,此時需要根據返回錯誤碼分析具體錯誤原因String videoId = response.getVideoId();if (!response.isSuccess()) {String errorMessage = "阿里云上傳錯誤:" + "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 服務上傳失敗");}} }5、創建controller
VideoAdminController.java
package com.south.vod.controller.admin;@Api(description="阿里云視頻點播微服務") @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);} }總結
- 上一篇: 家电行业售后服务管理系统有哪些黑科技应用
- 下一篇: 微信sdk相关配置