zoukankan      html  css  js  c++  java
  • 阿里云VOD 视频点播(三),后台java接口代码

    一,我使用的是SpringCloud微服框架开发接口,开发前准备一下几点

    (1),查看官方文档,获取相关信息,引入相关jar

    (1),Java上传SDK
    (2),服务端上传SDK下载

    注意:以下列举出部分依赖jar包的版本,您可直接在您的项目中添加maven依赖,也可以将VODUploadDemo-java-1.4.7.zip包中的所有jar包引入您的项目中使用。其中,aliyun-java-vod-upload-1.4.7.jar 还未正式开源,请您直接引入jar包至您的项目中使用。
    在这里插入图片描述

       <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.1.0</version>
        </dependency>
         <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-vod</artifactId>
            <version>2.15.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20170516</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>
    

    自己根据下载下来的jar,打包成maven,pom文件放在你的.m2里面然后在pom.xml,添加以下内容,

    打包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),下面是个人账号在项目中的配置,项目里面涉及三个接口,一个工具类,首先看下信息配置:

    vod:
      configs:
        #视频点播VOD,相关配置信息
        accessKeyId: 
        accessKeySecret:
        #点播服务接入区域
        regionId: cn-shanghai
    

    在这里插入图片描述
    定义properties文件,项目里面使用配置文件使用

    package io.renren.properties;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    /**
     * 视频点播VOD,相关配置信息
     *
     * @author lbx
     * @date 2018/12/10 10:59
     */
    @Component
    @Data
    @ConfigurationProperties(prefix = "vod.configs")
    public class VodProperties {
        private String accessKeyId;
        private String accessKeySecret;
        private String regionId;
    }
    
    

    (3),以下是三个controller接口,方法

    @Autowired
    
    private 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 = "获取播放凭证函数")
    	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);//播放凭证过期时间。取值范围: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;
        }
    
        /*获取播放凭证函数*/
        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;
        }
    }
    
    
  • 相关阅读:
    find ./ -type d ! -name "."
    Linux入门-进程、计划任务
    Linux入门-用户管理
    Linux入门-shell使用技巧
    Linux入门-压缩、解压
    Linux入门-常用命令
    MySQL杂项(索引注意事项 快速导入导出数据 锁 字符集 慢查询)
    MySQL分区实验
    Lvs网络负载均衡 直接路由(dr)
    Lvs网络负载均衡 隧道(ip tunl)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13300581.html
Copyright © 2011-2022 走看看