媒体上传

点播服务端提供丰富的上传接口,支持开发者采用不同的上传方式实现媒体上传。本文介绍Java SDK调用服务端上传接口的使用场景和调用示例。

使用场景

重要

本文仅提供API调用示例,即在点播服务中获取上传凭证和地址等操作示例。要实现完整的媒体文件上传逻辑,请参考下表跳转至对应的上传方式和使用场景。更多SDK问题咨询,可搜索钉钉群号2720012141入群咨询专家。

接口

使用场景

获取音视频上传地址和凭证

  • OSS原生SDK上传场景下,用户应用服务器集成点播服务端SDK并调用相关接口获取或刷新上传地址和凭证。获取到的上传地址和凭证需要Base64解码后作为初始化OSS上传客户端实例的参数。详情请参见基于OSS原生SDK上传

  • 点播客户端上传SDK场景下,用户应用服务器集成点播服务端SDK并调用相关接口获取或刷新上传地址和凭证。获取到的上传地址和凭证无需解码即可下发给客户端。详情请参见客户端上传SDK指引

刷新音视频上传凭证

获取图片上传地址和凭证

获取辅助媒资上传地址和凭证

URL批量拉取上传

  • 调用URL批量拉取上传接口实现离线拉取上传。这种方式可以免去先将文件下载到本地服务器或终端再使用上传SDK上传到点播的过程。

    说明

    URL拉取上传是异步任务,非实时,不保证时效性,一般提交后会在数小时、甚至数天内完成迁移上传。

  • 调用获取URL上传信息获取上传进度。

获取URL上传信息

前提条件

  • 调用接口前请先安装SDK。操作指引请参见安装

  • 调用接口前请先初始化客户端实例。操作指引请参见初始化

    说明

    以下示例均通过AccessKey初始化客户端实例。

接口调用示例

获取音/视频上传地址和凭证

调用CreateUploadVideo接口,完成获取音/视频上传地址和凭证功能。

接口参数和返回字段请参见CreateUploadVideo。调用示例如下:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;

/**
 *
 * 音频或视频获取上传凭证的示例
 */
public class AudioOrVideoCreateUploadDemo {

    //填入AccessKey信息
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入地域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * 获取音/视频上传地址和凭证
     * @param client 发送请求客户端
     * @return CreateUploadVideoResponse 获取音/视频上传地址和凭证响应数据
     * @throws Exception
     */
    public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client) throws Exception {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle("this is a sample");
        request.setFileName("filename.mp4");

        //UserData,用户自定义设置参数,用户需要单独回调URL及数据透传时设置(非必须)
        //JSONObject userData = new JSONObject();

        //UserData回调部分设置
        //消息回调设置,指定时以此为准,否则以全局设置的事件通知为准
        //JSONObject messageCallback = new JSONObject();
        //设置回调地址
        //messageCallback.put("CallbackURL", "http://192.168.0.1/16");
        //设置回调方式,默认为http
        //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());

        //request.setUserData(userData.toJSONString());

        return client.getAcsResponse(request);
    }

    // 请求示例
    public static void main(String[] argv) throws ClientException {
        try {
            DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
            CreateUploadVideoResponse response = new CreateUploadVideoResponse();
            response = createUploadVideo(client);
            System.out.print("VideoId = " + response.getVideoId() + "\n");
            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
    }
}

刷新音/视频上传凭证

调用RefreshUploadVideo接口,完成刷新音/视频上传凭证功能。

接口参数和返回字段请参见RefreshUploadVideo。调用示例如下:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;

/**
*
* 音频或视频刷新上传凭证的示例
*/
public class AudioOrVideoRefreshUploadDemo {

    //填入AccessKey信息
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入地域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

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

    // 请求示例
    public static void main(String[] argv)  {

        try {
            DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
            RefreshUploadVideoResponse response = refreshUploadVideo(client);
            System.out.print("VideoId = " + response.getVideoId() + "\n");
            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
    }
}

获取图片上传地址和凭证

调用CreateUploadImage接口,完成获取图片上传地址和凭证功能。

接口参数和返回字段请参见CreateUploadImage。调用示例如下:

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadImageRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadImageResponse;

/**
 * 图片获取上传凭证的示例
 *
 */
public class ImageCreateUploadDemo {

    //填入AccessKey信息
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入地域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * 获取图片上传地址和凭证
     * @param client 发送请求客户端
     * @return CreateUploadImageResponse 获取图片上传地址和凭证响应数据
     * @throws Exception
     */
    public static CreateUploadImageResponse createUploadImage(DefaultAcsClient client) throws Exception {
        CreateUploadImageRequest request = new CreateUploadImageRequest();
        //设置图片类型
        request.setImageType("default");
        //设置图片扩展名
        request.setImageExt("gif");
        //设置图片标题
        request.setTitle("this is a sample");

        //UserData,用户自定义设置参数,用户需要单独回调URL及数据透传时设置(非必须)
        JSONObject userData = new JSONObject();

        //UserData回调部分设置
        //消息回调设置,指定时以此为准,否则以全局设置的事件通知为准
        JSONObject messageCallback = new JSONObject();
        //设置回调地址
        messageCallback.put("CallbackURL", "http://192.168.0.0/16");
        //设置回调方式,默认为http
        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());

        return client.getAcsResponse(request);
    }

    // 请求示例
    public static void main(String[] argv)  {

        try {
            DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
            CreateUploadImageResponse response = createUploadImage(client);
            System.out.print("ImageId = " + response.getImageId() + "\n");
            System.out.print("ImageURL = " + response.getImageURL() + "\n");
            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
    }
}

获取辅助媒资上传地址和凭证

调用CreateUploadAttachedMedia接口,完成获取辅助媒资上传地址和凭证功能。

接口参数和返回字段请参见CreateUploadAttachedMedia。调用示例如下:

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadAttachedMediaRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadAttachedMediaResponse;

/**
 * 辅助媒资获取上传凭证的示例
 *
 */
public class AttachedMediaCreateUploadDemo {

    //填入AccessKey信息
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入地域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * 获取辅助媒资(水印、字幕等)上传地址和凭证
     * @param client 发送请求客户端
     * @return CreateUploadAttachedMediaResponse 获取辅助媒资上传地址和凭证响应数据
     * @throws Exception
     */
    public static CreateUploadAttachedMediaResponse createUploadAttachedMedia(DefaultAcsClient client) throws Exception {
        CreateUploadAttachedMediaRequest request = new CreateUploadAttachedMediaRequest();
        //设置业务类型
        request.setBusinessType("watermark");
        //设置文件扩展名
        request.setMediaExt("gif");
        //设置标题
        request.setTitle("this is a sample");

        //UserData,用户自定义设置参数,用户需要单独回调URL及数据透传时设置(非必须)
        JSONObject userData = new JSONObject();

        //UserData回调部分设置
        //消息回调设置,指定时以此为准,否则以全局设置的事件通知为准
        JSONObject messageCallback = new JSONObject();
        //设置回调地址
        messageCallback.put("CallbackURL", "http://192.168.0.0/16");
        //设置回调类型,默认为http
        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());

        return client.getAcsResponse(request);
    }
    // 请求示例
    public static void main(String[] argv) {

        try {
            DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
            CreateUploadAttachedMediaResponse response = createUploadAttachedMedia(client);
            System.out.print("mediaId = " + response.getMediaId() + "\n");
            System.out.print("mediaURL = " + response.getMediaURL() + "\n");
            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
    }
}

URL批量拉取上传

调用UploadMediaByURL接口,完成URL批量拉取上传功能。

接口参数和返回字段请参见UploadMediaByURL。调用示例如下:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.UploadMediaByURLRequest;
import com.aliyuncs.vod.model.v20170321.UploadMediaByURLResponse;

import java.net.URLEncoder;

/**
 * URL批量拉取上传功能示例
 *
 */
public class AudioOrVideoUploadByUrl {

    //填入AccessKey信息
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入地域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * URL批量拉取上传
     *
     * @param client 发送请求客户端
     * @return UploadMediaByURLResponse URL批量拉取上传响应数据
     * @throws Exception
     */
    public static UploadMediaByURLResponse uploadMediaByURL(DefaultAcsClient client) throws Exception {
        UploadMediaByURLRequest request = new UploadMediaByURLRequest();
        String url = "http://video_01.mp4";
        String encodeUrl = URLEncoder.encode(url, "UTF-8");
        //视频源文件URL
        request.setUploadURLs(encodeUrl);

        //上传视频元数据信息
        JSONObject uploadMetadata = new JSONObject();
        //需要上传的视频源文件URL,与UploadURLs里的URL匹配才能生效
        uploadMetadata.put("SourceUrl", encodeUrl);
        //视频标题
        uploadMetadata.put("Title", "upload by url sample");

        JSONArray uploadMetadataList = new JSONArray();
        uploadMetadataList.add(uploadMetadata);
        request.setUploadMetadatas(uploadMetadataList.toJSONString());

        //UserData,用户自定义设置参数,用户需要单独回调URL及数据透传时设置(非必须)
        JSONObject userData = new JSONObject();

        //UserData回调部分设置
        //消息回调设置,指定时以此为准,否则以全局设置的事件通知为准
        JSONObject messageCallback = new JSONObject();
        //设置回调地址
        messageCallback.put("CallbackURL", "http://192.168.0.0/16");
        //设置回调类型,默认为http
        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());

        return client.getAcsResponse(request);
    }

    // 请求示例
    public static void main(String[] argv) {

        try {
            DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
            UploadMediaByURLResponse response = uploadMediaByURL(client);
            System.out.print("UploadJobs = " + JSON.toJSONString(response.getUploadJobs()) + "\n");
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
    }

}

获取URL上传信息

调用GetURLUploadInfos接口,完成获取URL上传信息功能。

接口参数和返回字段请参见GetURLUploadInfos。调用示例如下:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetURLUploadInfosRequest;
import com.aliyuncs.vod.model.v20170321.GetURLUploadInfosResponse;
import org.apache.commons.lang3.StringUtils;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取URL上传信息示例
 */
public class URLUploadInfosGetDemo {
    
    //填入AccessKey信息
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入地域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * 获取URL上传信息
     *
     * @param client 发送请求客户端
     * @return GetURLUploadInfosResponse 获取URL上传信息响应数据
     * @throws Exception
     */
    public static GetURLUploadInfosResponse getURLUploadInfos(DefaultAcsClient client) throws Exception {
        GetURLUploadInfosRequest request = new GetURLUploadInfosRequest();

        //上传源视频URL列表,需要URLencode
        String[] urls = {
                "http://exampleBucket****.cn-shanghai.aliyuncs.com/video_01.mp4",
                "http://exampleBucket****.cn-shanghai.aliyuncs.com/video_02.flv"
        };
        List<String> encodeUrlList = new ArrayList<String>();
        for (String url : urls) {
            encodeUrlList.add(URLEncoder.encode(url, "UTF-8"));
        }
        request.setUploadURLs(StringUtils.join(encodeUrlList, ','));
        //JobId列表,可以通过GetPlayInfo接口中返回的PlayInfo结构体中获取
        //request.setJobIds("exampleID1,exampleID2");

        return client.getAcsResponse(request);
    }

    // 请求示例
    public static void main(String[] argv) {

        try {
            DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
            GetURLUploadInfosResponse response = getURLUploadInfos(client);
            System.out.print("URLUploadInfoList = " + response.getURLUploadInfoList() + "\n");
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
    }
}
阿里云首页 视频点播 相关技术圈