springboot整合阿里云视频点播接口

官方SDK文档地址: https://help.aliyun.com/document_detail/57756.html?spm=a2c4g.11186623.6.904.4e0d3bd9VbkICO

1、引入maven依赖

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.3.3</version>
  </dependency>
  <dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-vod</artifactId>
    <version>2.15.5</version>
  </dependency>
  <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
  </dependency>

 

2、application.yml配置

#阿里云accessKeyId
video_accessKeyId: LTAI
#阿里云accessKeySecret
video_accessKeySecret: kKUciySv0
#阿里云账号ID 值的来源https://help.aliyun.com/knowledge_detail/37196.html
video_userId: 103907

  

 

3、控制器类

AliyunVideoController.java

(RequestUtils为获取参数的工具类,可以不使用这种方式)

RequestUtils地址:https://www.cnblogs.com/pxblog/p/12238509.html

package cn.controller;

import cn.RequestUtils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class AliyunVideoController {

    @Value("${video_accessKeyId}")
    private String accessKeyId;

    @Value("${video_accessKeySecret}")
    private String accessKeySecret;

    @Value("${video_userId}")
    private String videoUserId;

    /**
     * 获取视频上传地址和凭证
     *
     */
    @RequestMapping("/aliyunVideo/createUploadVideo")
    @ResponseBody
    public void createUploadVideo(HttpServletRequest request, HttpServletResponse httpServletResponse) {
        DefaultAcsClient client = initVodClient();
        CreateUploadVideoResponse response = new CreateUploadVideoResponse();
        String title= RequestUtils.getQueryParam(request,"title");  //视频标题
        String fileName= RequestUtils.getQueryParam(request,"fileName");  //文件名称
        try {
            response = createUploadVideo(client,title,fileName);
            System.out.print("VideoId = " + response.getVideoId() + "\n");
            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }

    /**
     * 获取视频上传地址和凭证
     *
     * @param client 发送请求客户端
     * @return CreateUploadVideoResponse 获取视频上传地址和凭证响应数据
     * @throws Exception
     */
    public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client,String title,String fileName) {
        try {
            CreateUploadVideoRequest request = new CreateUploadVideoRequest();
            request.setTitle(title);
            request.setFileName(fileName);
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 刷新视频上传凭证
     *
     */
    @RequestMapping(value = "/aliyunVideo/refreshUploadVideo")
    @ResponseBody
    public void refreshUploadVideo(HttpServletRequest request){
        String videoId=RequestUtils.getQueryParam(request,"videoId");
        DefaultAcsClient client = initVodClient();
        RefreshUploadVideoResponse response = new RefreshUploadVideoResponse();
        try {
            response = refreshUploadVideo(client,videoId);
            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }

    /**
     * 刷新视频上传凭证
     *
     * @param client 发送请求客户端
     * @return RefreshUploadVideoResponse 刷新视频上传凭证响应数据
     * @throws Exception
     */
    public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient client,String videoId) throws Exception {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setVideoId(videoId);
        return client.getAcsResponse(request);
    }

    /**
     * 获取播放地址函数
     * 直接获取播放地址,可以使用任意播放器直接播放
     * @param request
     */
    @RequestMapping(value = "/aliyunVideo/getPlayInfo")
    @ResponseBody
    public void getPlayInfo(HttpServletRequest request){
        String videoId=RequestUtils.getQueryParam(request,"videoId");
        DefaultAcsClient client = initVodClient();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        try {
            response = getPlayInfo(client,videoId);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
            }
            //Base信息
            System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }

    /*获取播放地址函数*/
    public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client,String videoId) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        return client.getAcsResponse(request);
    }

    /**
     * 获取播放凭证函数
     * 必须使用阿里云播放器进行播放
     * @param request
     */
    @RequestMapping(value = "/aliyunVideo/getVideoPlayAuth")
    public void getVideoPlayAuth(HttpServletRequest request){
        String videoId=RequestUtils.getQueryParam(request,"videoId");
        DefaultAcsClient client = initVodClient();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        try {
            response = getVideoPlayAuth(client,videoId);
            //播放凭证
            System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
            //VideoMeta信息
            System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");

    }

    /*获取播放凭证函数*/
    public static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client,String videoId) throws Exception {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        return client.getAcsResponse(request);
    }

    @PostConstruct
    public  DefaultAcsClient initVodClient() {
        try {
            String regionId = "cn-shanghai";  // 点播服务接入区域
            DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            return client;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

  

原文地址:https://www.cnblogs.com/pxblog/p/12238695.html

时间: 2024-08-30 18:14:03

springboot整合阿里云视频点播接口的相关文章

在OneThink(ThinkPHP3.2.3)中整合阿里云OSS的PHP-SDK2.0.4,实现Web端直传,服务端签名直传并设置上传回调的实现流程

在OneThink(ThinkPHP3.2.3)中整合阿里云OSS的PHP-SDK2.0.4,实现本地文件上传流程 by shuijingwan · 2016/01/13 1.SDK安装 github地址:https://github.com/aliyun/aliyun-oss-php-sdk 2.复制aliyun-oss-php-sdk-master\src\OSS至passport.hmwis.com\ThinkPHP\Library\Vendor\OSS,如图1.2 复制aliyun-os

部署SpringBoot到阿里云

1.IDEA下载插件 Cloud Toolkit 或者直接在IDEA下载 2.进入 Preference 配置一个 Access Key ID 和 Access Key Secret 获取方法:弹出框选择继续使用 如果第一次打开需要新创建一个 点击显示即可 3.部署运行 4.远程登录阿里云启动 找到上面配置的文件,里面有生成的jar包 运行下面的代码 java -jar onepill_service-0.0.1-SNAPSHOT.jar 如果相应的端口没有开放去控制台开放 原文地址:https

springboot整合阿里druid数据源

全配置在application配置文件(方式1) maven依赖 <!--druid的启动器--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.17</version> </dependency> <!--这里不加jdbc依

wex5教程 js接入阿里天气api接口

一 前言 很多人问接入天气预报接口如何实现,很多天气接口没有提供js示例.今天以阿里云天气为例,对接口进行解读后,改装到js中实现数据接入.先看一下效果: 点击更新按钮,天气和图片显示到页面,数据调用成功. 二  阿里云天气接口注册与解读 1 购买阿里云天气免费版,可以用淘宝帐号购买,1000次调用次数和未来3天数据查询,对于测试完全可以满足. 2 得到appkey 3 阿里云api分析: 示例为根据id或地各查询7天天气.请求方式为GET,返回类型为json.身份认证为appcode. 请求参

阿里云发布多款云管工具,任何角色都可以轻松完成云上运维

摘要: 无论是在传统的开发过程,还是在云上,运维都是一个十分重要而又繁重的工作.随着企业规模的扩大,系统架构的复杂度在增加,部署规模也在不断扩大,控制台不再能满足其需求,需要一个便捷.实用的运维系统或者运维工具来完成,这不仅需要大量的开发工作,还需要对云产品的API进行学习和研究. 无论是在传统的开发过程,还是在云上,运维都是一个十分重要而又繁重的工作.随着企业规模的扩大,系统架构的复杂度在增加,部署规模也在不断扩大,控制台不再能满足其需求,需要一个便捷.实用的运维系统或者运维工具来完成,这不仅

阿里云 Aliplayer高级功能介绍(八):安全播放

基本介绍如何保障视频内容的安全,不被盗链.非法下载和传播,阿里云视频点播已经有一套完善的机制保障视频的安全播放: 更多详细内容查看点播内容安全播放,H5的Aliplayer对于上面的安全机制都是支持的,但是也有一些限制. 访问限制访问限制主要是阿里云视频云提供的安全访问能力, 只需要云端配置, 播放器无需做额外的事情,并且拒绝访问的原因会通过"X-Tengine-Error"Response Header返回,Http请求的错误的Code为403. 开启Referer防盗链后,如果Re

阿里云EDAS功能简介

尊敬的 EDAS 用户: 您好!为了给您带来更好的服务和使用体验,EDAS 产品团队将对 EDAS 标准版(含按量付费和包年包月)进行一轮调整,包括按量付费标准版价格和免费额度的更新,以及标准版套餐的功能升级. 价格调整 从 2019 年 7 月 1 日起, EDAS 按量付费标准版的价格将从原来1.00元/应用实例/天调整为 4.00 元/应用实例/天,并将免费使用的应用实例数量从原来的 2 个增加到 5 个,以便用户可以部署较复杂的微服务系统进行功能验证. 功能调整 标准版(含按量付费和包年

拿下 Gartner 容器产品第一,阿里云打赢云原生关键一战!

作者?| 易立(阿里云容器服务研发总监).伍杏玲 导读:近日,Gartner 发布 2020 年公共云容器报告.据报告显示,阿里云和 AWS 拥有最丰富的产品布局,覆盖 9 项产品能力,并列排名第一.具体详情可查看:<Gartner 容器报告:阿里云与 AWS 并列第一,领先微软.谷歌>. 据 Gartner 分析师评论,阿里云拥有丰富的容器产品形态,在中国市场表现强劲,在 Serverless 容器.服务网格.安全沙箱容器.混合云和边缘等 9 个产品领域具备良好的技术发展策略. 阿里云已连续

阿里云服务器win2003下iis整合tomcat共享80端口

阿里云服务器win2003下iis整合tomcat共享80端口 很多机器都用tomcat跟IIS部署不同网站.最近买了阿里云的服务器.于是也想玩一下.网上百度了很多方法.但是都有缺陷说的不是很清楚.通过日志查看以及谷歌.尝试了很多方法终于配置OK.         第一.整合环境:Windows Service 2003.JDK1.60.IIS6.0 和tomcat7.0         第二.使用技术:IIS6.0和TOMCAT6.0的默认端口不用改变,使用原有的80和8081(因为本人数据库