All Products
Search
Document Center

Media upload

Last Updated: Jul 11, 2019

This topic describes how to use the Java server SDK to call server operations, for example, to obtain the upload URL and credential and register media asset information. To upload complete media files, you can use the client upload SDK together with the upload URL and credential or directly use the Java server upload SDK.

Initialize a client

Initialize a client before using the SDK. For more information, see initVodClient.

Obtain the video upload URL and credential

For more information about the request and response parameters, see CreateUploadVideo.

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

/**
 * Method for obtaining the video upload URL and credential
 * @param client The client that sends a request.
 * @return CreateUploadVideoResponse The response to the request for obtaining the video upload URL and credential.
 * @throws Exception
*/
public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client) throws Exception {
    CreateUploadVideoRequest request = new CreateUploadVideoRequest();
    request.setTitle("this is a sample");
    request.setFileName("filename.mp4");

    JSONObject userData = new JSONObject();

    JSONObject messageCallback = new JSONObject();
    messageCallback.put("CallbackURL", "http://xxxxx");
    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);
}

// Call example
public static void main(String[] argv) {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    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");
}

Refresh the video upload credential

For more information about the request and response parameters, see RefreshUploadVideo.

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

/**
 * Method for refreshing the video upload credential
 * @param client The client that sends a request.
 * @return RefreshUploadVideoResponse The response to the request for refreshing the video upload credential.
 * @throws Exception
*/
public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient client) throws Exception {
    RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
    request.setVideoId("VideoId");
    return client.getAcsResponse(request);
}

// Call example
public static void main(String[] argv) {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    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");
}

Obtain the image upload URL and credential

For more information about the request and response parameters, see CreateUploadImage.

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

/**
 * Method for obtaining the image upload URL and credential
 * @param client The client that sends a request.
 * @return CreateUploadImageResponse The response to the request for obtaining the image upload URL and credential.
 * @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://xxxxx");
    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);
}

// Call example
public static void main(String[] argv) {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    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");
}

Obtain the upload URL and credential for an attached media asset

For more information about the request and response parameters, see CreateUploadAttachedMedia.

import com.aliyuncs.vod.model.v20170321.CreateUploadAttachedMediaRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadAttachedMediaResponse;
/**
 * Method for obtaining the upload URL and credential for an attached media asset (such as a watermark or subtitle file)
 * @param client The client that sends a request.
 * @return CreateUploadAttachedMediaResponse The response to the request for obtaining the upload URL and credential for an attached media asset.
 * @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://xxxxx");
    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);
}
// Call example
public static void main(String[] argv) {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    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");
}

Upload multiple videos based on mezzanine file URLs

For more information about the request and response parameters, see 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;

/**
 * Method for uploading multiple videos based on mezzanine file URLs
 * @param client The client that sends a request.
 * @return UploadMediaByURLResponse The response to the request for uploading multiple videos based on mezzanine file URLs.
 * @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://xxxxx");
    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);
}
// Call example
public static void main(String[] argv) {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    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");
}

Register media asset information

For more information about the request and response parameters, see 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;

/**
 * Method for registering media asset information
 * @param client The client that sends a request.
 * @return RegisterMediaResponse The response to the request for registering media asset information.
 * @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://xxxxxx.oss-cn-shanghai.aliyuncs.com/vod_sample.mp4");
    metaDataArray.add((metaData));
    request.setRegisterMetadatas(metaDataArray.toJSONString());
    return client.getAcsResponse(request);
}

// Call example
public static void main(String[] argv) throws Exception {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    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");
}

Obtain the information about upload based on mezzanine file URLs

For more information about the request and response parameters, see 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;

/**
 * Method for obtaining the information about upload based on mezzanine file URLs
 * @param client The client that sends a request.
 * @return GetURLUploadInfosResponse The response to the request for obtaining the information about upload based on mezzanine file URLs.
 * @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);
}
// Call example
public static void main(String[] argv) {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    GetURLUploadInfosResponse response = new GetURLUploadInfosResponse();
    try {
        response = getURLUploadInfos(client);
    } catch (Exception e) {
        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.print("RequestId = " + response.getRequestId() + "\n");
}