全部产品

媒体上传

本文档提供了Java SDK媒体上传相关功能的API调用示例,包含上传地址和凭证、注册媒资信息等。要实现完整的媒体文件上传,可配合客户端上传,也可以直接使用Java上传SDK。

初始化客户端

使用前请先初始化客户端,请参见初始

获取视频上传地址和凭证

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

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

import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;

/**
 * 获取视频上传地址和凭证
 * @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.0/16");
    //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) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    CreateUploadVideoResponse response = new CreateUploadVideoResponse();
    try {
        response = createUploadVideo(client);
        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");
}

刷新视频上传凭证

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

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

import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;

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

// 请求示例
public static void main(String[] argv) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    RefreshUploadVideoResponse response = new RefreshUploadVideoResponse();
    try {
        response = refreshUploadVideo(client);
        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");
}

获取图片上传地址和凭证

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

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

import com.aliyuncs.vod.model.v20170321.CreateUploadImageRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadImageResponse;

/**
 * 获取图片上传地址和凭证
 * @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");

    JSONObject userData = new JSONObject();

    JSONObject messageCallback = new JSONObject();
    messageCallback.put("CallbackURL", "http://192.168.0.0/16");
    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) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    CreateUploadImageResponse response = new CreateUploadImageResponse();
    try {
        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");
    } catch (Exception e) {
        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.print("RequestId = " + response.getRequestId() + "\n");
}

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

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

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

import com.aliyuncs.vod.model.v20170321.CreateUploadAttachedMediaRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadAttachedMediaResponse;
/**
 * 获取辅助媒资(水印、字幕等)上传地址和凭证
 * @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");

    JSONObject userData = new JSONObject();

    JSONObject messageCallback = new JSONObject();
    messageCallback.put("CallbackURL", "http://192.168.0.0/16");
    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) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    CreateUploadAttachedMediaResponse response = new CreateUploadAttachedMediaResponse();
    try {
        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");
    } catch (Exception e) {
        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.print("RequestId = " + response.getRequestId() + "\n");
}

URL批量拉取上传

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

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.vod.model.v20170321.UploadMediaByURLRequest;
import com.aliyuncs.vod.model.v20170321.UploadMediaByURLResponse;

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

    JSONObject uploadMetadata = new JSONObject();
    uploadMetadata.put("SourceUrl", encodeUrl);
    uploadMetadata.put("Title", "upload by url sample");

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

    JSONObject userData = new JSONObject();

    JSONObject messageCallback = new JSONObject();
    messageCallback.put("CallbackURL", "http://192.168.0.0/16");
    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) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    UploadMediaByURLResponse response = new UploadMediaByURLResponse();
    try {
        response = uploadMediaByURL(client);
        System.out.print("UploadJobs = " + JSON.toJSONString(response.getUploadJobs()) + "\n");
    } catch (Exception e) {
        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.print("RequestId = " + response.getRequestId() + "\n");
}

注册媒资信息

调用RegisterMedia接口,完成注册媒资信息功能。

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

import com.aliyuncs.vod.model.v20170321.RegisterMediaRequest;
import com.aliyuncs.vod.model.v20170321.RegisterMediaResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 注册媒资信息
 * @param client 发送请求客户端
 * @return RegisterMediaResponse 注册媒资信息响应数据
 * @throws Exception
*/
public static RegisterMediaResponse registerMedia(DefaultAcsClient client) throws Exception {
    RegisterMediaRequest request = new RegisterMediaRequest();
    JSONArray metaDataArray = new JSONArray();
    JSONObject metaData = new JSONObject();
    metaData.put("Title", "this is a sample");
    metaData.put("FileURL", "https://192.168.0.0/16.oss-cn-shanghai.aliyuncs.com/vod_sample.mp4");
    metaDataArray.add((metaData));
    request.setRegisterMetadatas(metaDataArray.toJSONString());
    return client.getAcsResponse(request);
}

// 请求示例
public static void main(String[] argv) throws Exception {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    RegisterMediaResponse response = new RegisterMediaResponse();
    try {
        response = registerMedia(client);
        if (response.getFailedFileURLs() != null && response.getFailedFileURLs().size() > 0) {
            for (String fileURL : response.getFailedFileURLs()) {
                System.out.print("FailedFileURL = " + fileURL + "\n");
            }
        }
        if (response.getRegisteredMediaList() != null && response.getRegisteredMediaList().size() > 0) {
        for (RegisterMediaResponse.RegisteredMedia registeredMedia : response.getRegisteredMediaList()) {
            System.out.print("MediaId = " + registeredMedia.getMediaId());
            System.out.print("FileURL = " + registeredMedia.getFileURL());
            System.out.print("NewRegister = " + registeredMedia.getNewRegister());
            }
        }
    } catch (Exception e) {
        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.print("RequestId = " + response.getRequestId() + "\n");
}

获取URL上传信息

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

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

import java.net.URLEncoder;
import java.util.Arrays;
import org.apache.commons.lang.StringUtils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetURLUploadInfosRequest;
import com.aliyuncs.vod.model.v20170321.GetURLUploadInfosResponse;

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

    String[] urls = {
        "http://xxx.cn-shanghai.aliyuncs.com/sample1.mp4",
        "http://xxx.cn-shanghai.aliyuncs.com/sample2.flv"
    };
    List<String> encodeUrlList = new ArrayList<String>();
    for(String url : urls){
        encodeUrlList.add(URLEncoder.encode(url, "UTF-8"));
    }
    request.setUploadURLs(StringUtils.join(encodeUrlList, ','));
    //request.setJobIds("xxx1,xxx2");

    return client.getAcsResponse(request);
}
// 请求示例
public static void main(String[] argv) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    GetURLUploadInfosResponse response = new GetURLUploadInfosResponse();
    try {
        response = getURLUploadInfos(client);
    } catch (Exception e) {
        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.print("RequestId = " + response.getRequestId() + "\n");
}