不得不佩服阿里的东西简单粗暴,从看api到代码完成2天搞定
package com.zhaohua.common.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.UpdateVideoInfoRequest;
import com.aliyuncs.vod.model.v20170321.UpdateVideoInfoResponse;
import com.zhaohua.nkt.vo.VideoUploadBean;
@Configuration
public class AliyunUtil {
public static String accessKeyID;
public static String accessKeySecret;
@Value("${aliyun.accessKeyID}")
public void setAccessKeyID(String accessKeyID) {
AliyunUtil.accessKeyID = accessKeyID;
}
@Value("${aliyun.accessKeySecret}")
public void setAccessKeySecret(String accessKeySecret) {
AliyunUtil.accessKeySecret = accessKeySecret;
}
/**
* 初始化客户端(获取key和secret)
*
*/
public static DefaultAcsClient initVodClient() {
//点播服务所在的Region,国内请填cn-shanghai,不要填写别的区域
String regionId = "cn-shanghai";
DefaultProfile profile = DefaultProfile.getProfile(regionId, AliyunUtil.accessKeyID, AliyunUtil.accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* 获取播放凭证
*
* @param videoId 视频id
* @return GetVideoPlayAuthResponse
* @throws ClientException
* @throws ServerException
*/
public static GetVideoPlayAuthResponse getVideoPlayAuth(String videoId) throws ServerException, ClientException {
DefaultAcsClient client = AliyunUtil.initVodClient();
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
request.setVideoId(videoId);
GetVideoPlayAuthResponse response = client.getAcsResponse(request);
return response;
}
/**
* 获取视频上传地址和凭证函数
* @param VideoUploadBean videoUploadBean
*/
public static CreateUploadVideoResponse createUploadVideo(VideoUploadBean videoUploadBean) throws Exception {
DefaultAcsClient client = AliyunUtil.initVodClient();
CreateUploadVideoRequest request = new CreateUploadVideoRequest();
request.setTitle(videoUploadBean.getTitle());
request.setDescription(videoUploadBean.getDescription());
request.setFileName(videoUploadBean.getFileName());
request.setTags(videoUploadBean.getTags());
request.setCoverURL(videoUploadBean.getCoverUrl());
return client.getAcsResponse(request);
}
/**
* 刷新视频上传凭证
* @param DefaultAcsClient client
* @param videoId 视频id
* @return RefreshUploadVideoResponse
*/
public static RefreshUploadVideoResponse refreshUploadVideo(String videoId) throws Exception {
DefaultAcsClient client = AliyunUtil.initVodClient();
RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
request.setVideoId(videoId);
return client.getAcsResponse(request);
}
/**
* 修改视频信息
*
* @param videoId 视频id
* @return GetVideoInfoResponse
*/
public static UpdateVideoInfoResponse updateVideoInfo(VideoUploadBean videoUploadBean) throws Exception {
DefaultAcsClient client = AliyunUtil.initVodClient();
UpdateVideoInfoRequest request = new UpdateVideoInfoRequest();
request.setVideoId(videoUploadBean.getVideoId());
request.setTitle(videoUploadBean.getTitle());
request.setDescription(videoUploadBean.getDescription());
request.setTags(videoUploadBean.getTags());
return client.getAcsResponse(request);
}
/**
* 删除视频函数
*
* @param DefaultAcsClient client
* @param videoId 视频id
* @return GetVideoInfoResponse
*/
public static DeleteVideoResponse deleteVideo(String videoIds) throws Exception {
DefaultAcsClient client = AliyunUtil.initVodClient();
DeleteVideoRequest request = new DeleteVideoRequest();
//多个用逗号分隔,最多支持20个
// request.setVideoIds("视频ID1,视频ID2");
request.setVideoIds(videoIds);
return client.getAcsResponse(request);
}
/**
* 获取视频信息
*
* @param videoId 视频id
* @return GetVideoInfoResponse
*/
public static GetVideoInfoResponse getVideoInfo(String videoId) throws Exception {
DefaultAcsClient client = AliyunUtil.initVodClient();
GetVideoInfoRequest request = new GetVideoInfoRequest();
request.setVideoId(videoId);
return client.getAcsResponse(request);
}
/**
* 获取播放信息(包括播放地址)
*
* @param videoId 视频id
* @return GetVideoInfoResponse
*/
public static GetPlayInfoResponse getPlayInfo(String videoId) throws Exception {
DefaultAcsClient client = AliyunUtil.initVodClient();
GetPlayInfoRequest request = new GetPlayInfoRequest();
request.setVideoId(videoId);
return client.getAcsResponse(request);
}
}
package com.zhaohua.nkt.controller;import java.util.List;import javax.validation.Valid;import org.apache.log4j.Logger;import org.springframework.validation.BindingResult;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import com.alibaba.fastjson.JSONObject;import com.aliyuncs.exceptions.ClientException;import com.aliyuncs.exceptions.ServerException;import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse;import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse.Video;import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;import com.zhaohua.common.util.AliyunUtil;import com.zhaohua.framework.shiro.entity.RestEntity;import com.zhaohua.nkt.vo.VideoUploadBean;/** * 和阿里云视频相关的操作 * * @author 尹东亮 */@RestController@RequestMapping("/aliyun")public class AliyunController {private Logger logger = Logger.getLogger(getClass());/** * 刷新上传凭证 3000秒过期时间 * * @throws Exception */@RequestMapping(value = "/refreshUploadAuten/{videoId}", method = RequestMethod.GET)public Object refreshUploadAuten(@PathVariable String videoId) throws Exception {RefreshUploadVideoResponse response = AliyunUtil.refreshUploadVideo(videoId);JSONObject jsonObject = new JSONObject();jsonObject.put("uploadAddress", response.getUploadAddress());jsonObject.put("uploadAuth", response.getUploadAuth());return RestEntity.success(jsonObject);}/** * 获取视频上传地址和凭证 * * @param videoUploadBean * @throws Exception * */@RequestMapping(value = "/getUploadVideoUrlAuten", method = RequestMethod.POST)public Object getUploadVideoUrlAuten(@RequestBody VideoUploadBean videoUploadBean) throws Exception {CreateUploadVideoResponse response = AliyunUtil.createUploadVideo(videoUploadBean);JSONObject jsonObject = new JSONObject();jsonObject.put("videoId", response.getVideoId());jsonObject.put("uploadAddress", response.getUploadAddress());jsonObject.put("uploadAuth", response.getUploadAuth());return RestEntity.success(jsonObject);}/** * 删除视频,(最多一次可以删除20个) * * @throws Exception * */@RequestMapping(value = "/delete/{videoId}", method = RequestMethod.GET)public Object delete(@PathVariable String videoId) throws Exception {AliyunUtil.deleteVideo(videoId);return RestEntity.success("删除成功");}/** * 修改视频信息 * * @throws Exception * */@RequestMapping(value = "/update", method = RequestMethod.POST)public Object update(@RequestBody @Valid VideoUploadBean videoUploadBean, BindingResult bindingResult) throws Exception {if (bindingResult.hasErrors()) {StringBuffer sbf = new StringBuffer();bindingResult.getAllErrors().forEach(e -> {logger.error(e.getDefaultMessage());sbf.append(e.getDefaultMessage()).append(";");});return RestEntity.failed(sbf.toString());}AliyunUtil.updateVideoInfo(videoUploadBean);return RestEntity.success("更新成功");}/** * 获取单个视频信息 * * @throws Exception * */@RequestMapping(value = "/getVideoInfo/{videoId}", method = RequestMethod.GET)public Object getUploadImgUrlAuten(@PathVariable String videoId) throws Exception {GetVideoInfoResponse response = AliyunUtil.getVideoInfo(videoId);Video video=response.getVideo();return RestEntity.success(video);}/** * 获取临时播放url(url播放方式) * @throws Exception * * @throws ClientException * @throws ServerException * */@RequestMapping(value = "/getTempPlayUrl/{videoId}", method = RequestMethod.GET)public Object getTempPlayUrl(@PathVariable String videoId) throws Exception {GetPlayInfoResponse response = AliyunUtil.getPlayInfo(videoId);List playInfoList = response.getPlayInfoList();
// 播放地址
String playURL=null;
for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
if(("mp4").equals(playInfo.getFormat())) {
playURL= playInfo.getPlayURL();
break;
}
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("playURL", playURL);
return RestEntity.success(jsonObject);
}
/**
* 获取播放凭证(凭证播放方式)
* 100秒过期
* @throws ClientException
* @throws ServerException
*
*/
@RequestMapping(value = "/getPlayAuten/{videoId}", method = RequestMethod.GET)
public Object getPlayAuten(@PathVariable String videoId) throws ServerException, ClientException {
GetVideoPlayAuthResponse response = AliyunUtil.getVideoPlayAuth(videoId);
JSONObject jsonObject = new JSONObject();
jsonObject.put("palyAuten", response.getPlayAuth());
return RestEntity.success(jsonObject);
}
}
网友评论