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: 10
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;
}
}
springboot整合阿里云视频点播接口的更多相关文章
- SpringBoot整合阿里云OSS文件上传、下载、查看、删除
1. 开发前准备 1.1 前置知识 java基础以及SpringBoot简单基础知识即可. 1.2 环境参数 开发工具:IDEA 基础环境:Maven+JDK8 所用技术:SpringBoot.lom ...
- SpringBoot整合阿里云OSS对象存储实现文件上传
1. 准备工作: 一.首先登录阿里云OSS对象存储控制台创建一个Bucket作为你的存储空间. 二.创建Access Keyan按要求创建进行,这里的方法步骤我就不展现出来了,你们可以自行查询阿里云文 ...
- SpringBoot整合阿里短信服务
导读 由于最近手头上需要做个Message Gateway,涉及到:邮件(点我直达).短信.公众号(点我直达)等推送功能,网上学习下,整理下来以备以后使用. 步骤 点我直达 登录短信服务控制台 点我直 ...
- 阿里云视频点播之URL批量拉取上传(调整为多个视频上传)
项目引入阿里云视频点播PHP-SDK 背景:2021年乐视云的点播将停止提供服务,项目决定选择选用阿里云的视频的点播.在上线前,需要将之前的视频提前导入资源库,URLS方式拉取是比较方便的,对编辑同事 ...
- 整合阿里云OSS
整合阿里云OSS 一.对象存储OSS 为了解决海量数据存储与弹性扩容,采用云存储的解决方案- 阿里云OSS. 1.开通"对象存储OSS"服务 (1)申请阿里云账号 (2)实名认证 ...
- 在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 ...
- Thinkphp整合阿里云OSS图片上传实例
Thinkphp3.2整合阿里云OSS图片上传实例,图片上传至OSS可减少服务器压力,节省宽带,安全又稳定,阿里云OSS对于做负载均衡非常方便,不用传到各个服务器了 首先引入阿里云OSS类库 < ...
- SpringBoot整合阿里Druid数据源及Spring-Data-Jpa
SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...
- 通过AccessKey调用阿里云CDN接口刷新CDN资源案例
通过AccessKey远程调用阿里云CDN接口,快速实现自动化集成部署. CdnService.java package com.nfky.cdn; import com.aliyuncs.Defau ...
随机推荐
- 【2020五校联考NOIP #6】最佳观影
题意: 给出一个 \(k \times k\) 的网格和 \(n\) 次操作.其中 \(k\) 为奇数. 每次操作给出一个数 \(m\).每次你要找出一个三元组 \((x,l,r)\) 使得: \(r ...
- Codeforces 407E - k-d-sequence(单调栈+扫描线+线段树)
Codeforces 题面传送门 & 洛谷题面传送门 深感自己线段树学得不扎实-- 首先特判掉 \(d=0\) 的情况,显然这种情况下满足条件的区间 \([l,r]\) 中的数必须相同,双针扫 ...
- Atcoder Regular Contest 093 D - Dark Horse(组合数学+状压 dp)
Atcoder 题面传送门 & 洛谷题面传送门 常规题,简单写写罢((( 首先 \(1\) 的位置是什么不重要,我们不妨钦定 \(1\) 号选手最初就处在 \(1\) 号位置,最后答案乘个 \ ...
- CF1466G Song of the Sirens
题目传送门 题意简述:给出 \(n,s_0,t\ (n=|t|)\),定义 \(s_i=s_{i-1}+t_i+s_{i-1}\).多次询问给出 \(k,m\),求 \(m\) 在 \(s_k\) 中 ...
- 模仿UP主,用Python实现一个弹幕控制的直播间!
灵感来源 之前在B站看到一个有意思的视频: [B站][亦]终极云游戏!五千人同开一辆车,复现经典群体智慧实验 大家可以看看,很有意思. up主通过代码实现了实时读取直播间里的弹幕内容,进而控制自己的电 ...
- Session和Cookie的原理,以及在分布式应用中出现的问题和解决方案
产生原因 由于http协议是无状态的,同一个浏览器对服务器的两次请求之间是没有关系的,服务器认为两次请求都是全新的请求,不会记住上次请求成功的数据.然而现有的业务常常需要服务器能记住用户的访问情况, ...
- ubuntu 常用指令
1.进入到root权限的指令 sudo su,效果同su,只是不需要root的密码,而需要当前用户的密码.(亲测有效) 2.从root权限里面退出到 普通用户模式 exit---指令亲测有效 3.下载 ...
- SpringBoot 整合 MyBatis,实现 CRUD 示例
目录 前言 创建项目/模块 SpringBoot Console Application CommandLineRunner SpringBoot 集成 MyBatis 创建数据库/表 配置数据源/连 ...
- 学习java 7.22
学习内容: GridBagLayout GridBagLayout布局管理器的功能最强大,但也最复杂,与GridLayout布局管理器不同的是,在GridBagLayout布局管理器中,一个组件可以跨 ...
- 零基础学习java------30---------wordCount案例(涉及到第三种多线程callable)
知识补充:多线程的第三种方式 来源:http://www.threadworld.cn/archives/39.html 创建线程的两种方式,一种是直接继承Thread,另外一种就是实现Runnabl ...