All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zopen.aliyun.vod.service.AliyunVodUploadService Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package com.zopen.aliyun.vod.service;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.zopen.aliyun.exception.AliyunAssert;
import com.zopen.aliyun.exception.AliyunException;
import com.zopen.aliyun.vod.dto.AliyunVodPreUploadResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("atoAliyunVodUploadService")
public class AliyunVodUploadService {

    private static final Logger logger = LoggerFactory.getLogger(AliyunVodUploadService.class);

    @Autowired
    private DefaultAcsClient client;

    /**
     * 获取视频上传地址和凭证
     *
     * @param title    * 视频标题,例:this is a sample
     * @param fileName * 视频文件,例:filename.mp4
     * @param userData 用户自定义设置参数,用户需要单独回调 URL 及数据透传时设置
     * @return 视频上传地址和凭证
     */
    public AliyunVodPreUploadResp createUploadVideo(String title, String fileName, String userData) {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(title);
        request.setFileName(fileName);

        /// UserData,用户自定义设置参数,用户需要单独回调URL及数据透传时设置(非必须)
        //JSONObject userData = new JSONObject();
        //UserData回调部分设置
        //JSONObject messageCallback = new JSONObject();
        //messageCallback.put("CallbackURL", "http://xxxxx");
        //messageCallback.put("CallbackType", "http");
        //userData.put("MessageCallback", messageCallback.toJSONString());
        //UserData透传数据部分设置
        //JSONObject extend = new JSONObject();
        //extend.put("MyId", "user-defined-id");
        //userData.put("Extend", extend.toJSONString());
        //userData.toJSONString()

        request.setUserData(userData);
        CreateUploadVideoResponse response = null;
        try {
            logger.debug(">>发起获取视频上传地址和凭证请求:title[{}]、fileName[{}]、userData[{}]", title, fileName, userData);
            response = client.getAcsResponse(request);
            logger.debug("<<获取视频上传地址和凭证请求返回:videoId[{}]、uploadAddress[{}]、uploadAuth[{}]",
                    response.getVideoId(), response.getUploadAddress(), response.getUploadAuth());
            return new AliyunVodPreUploadResp(response);
        } catch (Exception e) {
            logger.error("<<获取视频上传地址和凭证失败:" + e.getMessage(), e);
            throw new AliyunException("获取视频上传地址和凭证失败:" + e.getMessage());
        }
    }

    /**
     * 刷新视频上传凭证
     *
     * @param videoId * 视频ID
     * @return 视频上传凭证
     */
    public AliyunVodPreUploadResp refreshUploadVideo(String videoId) {
        AliyunAssert.notNullAndEmpty(videoId, "videoId 不能为空");
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setVideoId(videoId);
        RefreshUploadVideoResponse response = null;
        try {
            logger.debug(">>发起刷新视频上传凭证请求:videoId[{}]", videoId);
            response = client.getAcsResponse(request);
            logger.debug("<<刷新视频上传凭证请求返回:videoId[{}]、uploadAddress[{}]、uploadAuth[{}]",
                    response.getVideoId(), response.getUploadAddress(), response.getUploadAuth());
            return new AliyunVodPreUploadResp(response);
        } catch (Exception e) {
            logger.error("<<刷新视频上传凭证失败:" + e.getMessage(), e);
            throw new AliyunException("刷新视频上传凭证失败:" + e.getMessage());
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy