阿里云VOD 视频点播(三),后台java接口代码
生活随笔
收集整理的這篇文章主要介紹了
阿里云VOD 视频点播(三),后台java接口代码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一,我使用的是SpringCloud微服框架開發(fā)接口,開發(fā)前準(zhǔn)備一下幾點(diǎn)
(1),查看官方文檔,獲取相關(guān)信息,引入相關(guān)jar
(1),Java上傳SDK
(2),服務(wù)端上傳SDK下載
注意:以下列舉出部分依賴jar包的版本,您可直接在您的項(xiàng)目中添加maven依賴,也可以將VODUploadDemo-java-1.4.7.zip包中的所有jar包引入您的項(xiàng)目中使用。其中,aliyun-java-vod-upload-1.4.7.jar 還未正式開源,請您直接引入jar包至您的項(xiàng)目中使用。
自己根據(jù)下載下來的jar,打包成maven,pom文件放在你的.m2里面然后在pom.xml,添加以下內(nèi)容,
打包pom,請查看如下地址:maven如何將本地jar安裝到本地倉庫
<dependency><groupId>com.aliyun-vod-upload.specs</groupId><artifactId>aliyun-java-vod-upload-201905014</artifactId><version>1.4.7</version></dependency>(2),下面是個(gè)人賬號在項(xiàng)目中的配置,項(xiàng)目里面涉及三個(gè)接口,一個(gè)工具類,首先看下信息配置:
vod:configs:#視頻點(diǎn)播VOD,相關(guān)配置信息accessKeyId: accessKeySecret:#點(diǎn)播服務(wù)接入?yún)^(qū)域regionId: cn-shanghai
定義properties文件,項(xiàng)目里面使用配置文件使用
(3),以下是三個(gè)controller接口,方法
@Autowiredprivate VodProperties vodProperties;@GetMapping("createUploadVideo")@ApiOperation(value = "獲取視頻上傳地址和憑證")public Result<Map<String, Object>> createUploadVideo(@ApiIgnore @RequestParam Map<String, Object> params){params.put("regionId",vodProperties.getRegionId());params.put("accessKeyId",vodProperties.getAccessKeyId());params.put("accessKeySecret",vodProperties.getAccessKeySecret());Map<String, Object> resultMap = AliyunUpload.createUploadVideo(params);return new Result<Map<String, Object>>().ok(resultMap);}@GetMapping("refreshUploadVideo")@ApiOperation(value = "刷新視頻上傳憑證")public Result<Map<String, Object>> refreshUploadVideo(@ApiIgnore @RequestParam Map<String, Object> params){params.put("regionId",vodProperties.getRegionId());params.put("accessKeyId",vodProperties.getAccessKeyId());params.put("accessKeySecret",vodProperties.getAccessKeySecret());Map<String, Object> resultMap = AliyunUpload.refreshUploadVideo(params);return new Result<Map<String, Object>>().ok(resultMap);}@GetMapping("getVideoPlayAuth")@ApiOperation(value = "獲取播放憑證函數(shù)")public Result<Map<String, Object>> getVideoPlayAuth(@ApiIgnore @RequestParam Map<String, Object> params){params.put("regionId",vodProperties.getRegionId());params.put("accessKeyId",vodProperties.getAccessKeyId());params.put("accessKeySecret",vodProperties.getAccessKeySecret());params.put("authInfoTimeout",3000);//播放憑證過期時(shí)間。取值范圍:100~3000。Map<String, Object> resultMap = AliyunUpload.getVideoPlayAuth(params);return new Result<Map<String, Object>>().ok(resultMap);}(4),以下是使用的工具類
package io.renren.utils;import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.vod.model.v20170321.*; import io.renren.properties.VodProperties; import org.springframework.beans.factory.annotation.Autowired;import java.util.HashMap; import java.util.Map;/*** @Author: LBX* @Date: 2019/5/14 16:09*/ public class AliyunUpload {//初始化客戶端public static DefaultAcsClient initVodClient(Map<String, Object> param) {DefaultProfile profile = DefaultProfile.getProfile(param.get("regionId").toString(), param.get("accessKeyId").toString(), param.get("accessKeySecret").toString());DefaultAcsClient client = new DefaultAcsClient(profile);return client;}/*** 獲取視頻上傳地址和憑證*/public static HashMap<String, Object> createUploadVideo(Map<String, Object> param) {DefaultAcsClient client = initVodClient(param);CreateUploadVideoRequest request = new CreateUploadVideoRequest();request.setTitle(param.get("title").toString());request.setFileName(param.get("fileName").toString());//JSONObject userData = new JSONObject();//JSONObject messageCallback = new JSONObject();//messageCallback.put("CallbackURL", "http://xxxxx");//messageCallback.put("CallbackType", "http");//userData.put("MessageCallback", messageCallback.toJSONString());//JSONObject extend = new JSONObject();//extend.put("MyId", "user-defined-id");//userData.put("Extend", extend.toJSONString());//request.setUserData(userData.toJSONString());HashMap<String, Object> map = new HashMap<>();try {CreateUploadVideoResponse response = client.getAcsResponse(request);map.put("Code", "0");map.put("VideoId", response.getVideoId());map.put("UploadAddress", response.getUploadAddress());map.put("UploadAuth", response.getUploadAuth());map.put("RequestId", response.getRequestId());} catch (ClientException e) {e.printStackTrace();map.put("Code", "1");map.put("ErrorMessage", e.getLocalizedMessage());}return map;}/*** 刷新視頻上傳憑證*/public static Map<String, Object> refreshUploadVideo(Map<String, Object> param) {DefaultAcsClient client = initVodClient(param);RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();request.setVideoId(param.get("videoId").toString());Map<String, Object> map = new HashMap<>();try {RefreshUploadVideoResponse response = client.getAcsResponse(request);map.put("Code", "0");map.put("VideoId", response.getVideoId());map.put("UploadAddress", response.getUploadAddress());map.put("UploadAuth", response.getUploadAuth());} catch (ClientException e) {e.printStackTrace();map.put("Code", "1");map.put("ErrorMessage", e.getLocalizedMessage());}return map;}/*獲取播放憑證函數(shù)*/public static Map<String, Object> getVideoPlayAuth(Map<String, Object> param) {DefaultAcsClient client = initVodClient(param);GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();request.setVideoId(param.get("videoId").toString());request.setAuthInfoTimeout(Long.valueOf(param.get("authInfoTimeout").toString()));Map<String, Object> map = new HashMap<>();try {GetVideoPlayAuthResponse response = client.getAcsResponse(request);map.put("Code", "0");map.put("PlayAuth", response.getPlayAuth());map.put("RequestId", response.getRequestId());map.put("VideoId", response.getVideoMeta().getVideoId());map.put("Title", response.getVideoMeta().getTitle());map.put("CoverUrl", response.getVideoMeta().getCoverURL());} catch (ClientException e) {e.printStackTrace();map.put("Code", "1");map.put("ErrorMessage", e.getLocalizedMessage());}return map;} }總結(jié)
以上是生活随笔為你收集整理的阿里云VOD 视频点播(三),后台java接口代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python字典合并去重_十三、深入Py
- 下一篇: c语言编写五十以内加减法,求用C编个大数