All Products
Search
Document Center

Media upload

Last Updated: Apr 18, 2022

ApsaraVideo VOD servers provide a variety of API operations for media upload. You can upload media files to ApsaraVideo VOD by calling one of these API operations. This topic describes the scenarios in which you can use the server operation SDK for Java to call these API operations and provides sample code.

Scenarios

Notice

This topic provides only the sample code of calling API operations, such as the operation for obtaining a URL and a credential for uploading a media file. For more information about the scenarios in which the operations can be used, see the following table.

Operation

Scenario

CreateUploadVideo

  • If you upload media files by using Object Storage Service (OSS) SDKs, you need to use the server operation SDK and call an operation to obtain a URL and a credential for uploading a media file. The obtained URL and credential are Base64-encoded and must be decoded before they are used to initialize an OSS client. For more information, see Upload media files by using OSS SDKs.

  • If you upload media file by using ApsaraVideo VOD clients, you need to use the server operation SDK and call an operation to obtain a URL and a credential for uploading a media file. You can deliver the URL and credential to clients without the need to decode the URL or credential. For more information, see Upload from clients.

RefreshUploadVideo

CreateUploadImage

CreateUploadAttachedMedia

UploadMediaByURL

  • You can call the UploadMediaByURL operation to upload media files by using the URLs of source files. This way, you do not need to download media files to your servers or terminals before you use the server operation SDK to upload the media files to ApsaraVideo VOD.

  • You can call the GetURLUploadInfos operation to query the information about URL-based upload jobs.

GetURLUploadInfos

RegisterMedia

  • After you upload media files to a user-created OSS bucket, you can call the RegisterMedia operation to register the media files and obtain the media IDs of the files. After you register the media files, you can submit transcoding or snapshot jobs for the media files. For more information, see Add a user-created OSS bucket.

Prerequisites

  • The server operation SDK for Java is installed. For more information, see Installation.

  • The server operation SDK for Java is initialized. For more information, see Initialization.

    Note

    In the following examples, an AccessKey pair is used to initialize the server operation SDK.

Sample code

Obtain a URL and a credential for uploading an audio file or a video

You can call the CreateUploadVideo operation to obtain a URL and a credential for uploading an audio file or a video.

For more information about the request and response parameters of this operation, see CreateUploadVideo. The following sample code provides an example:

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;

/**
 *
 * The sample code of obtaining a URL and a credential for uploading an audio file or a video.
 */
public class AudioOrVideoCreateUploadDemo {

    // Specify the AccessKey pair.
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // Specify the region from which you want to access ApsaraVideo VOD.
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * Obtain a URL and a credential for uploading an audio file or a video.
     * @param client The client that sends a request.
     * @return CreateUploadVideoResponse The fields that must be contained in the response.
     * @throws Exception
     */
    public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client) throws Exception {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle("this is a sample");
        request.setFileName("filename.mp4");

        // Optional. The user data that consists of user-defined parameters. If you need a callback URL and transparent data transmission, you can configure the user data.
        //JSONObject userData = new JSONObject();

        // The callback configurations in the user data.
        // The callback configurations for event notifications. If you specify callback configurations, the specified callback configurations take effect. Otherwise, the global callback configurations take effect.
        //JSONObject messageCallback = new JSONObject();
        // Configure a callback URL.
        //messageCallback.put("CallbackURL", "http://192.168.0.1/16");
        // Configure the callback type. The default type is HTTP.
        //messageCallback.put("CallbackType", "http");
        //userData.put("MessageCallback", messageCallback.toJSONString());

        // The configurations of transparent data transmission in the user data.
        // The user-defined extension parameter, which is transparently transferred during the callback.
        //JSONObject extend = new JSONObject();
        //extend.put("MyId", "user-defined-id");
        //userData.put("Extend", extend.toJSONString());

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

        return client.getAcsResponse(request);
    }

    // The sample code of sending a 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());
        }
    }
}

Refresh the credential for uploading an audio file or a video

You can call the RefreshUploadVideo operation to refresh the credential for uploading an audio file or a video.

For more information about the request and response parameters of this operation, see RefreshUploadVideo. The following sample code provides an example:

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;

/**
*
* The sample code of refreshing the credential for uploading an audio file or a video.
*/
public class AudioOrVideoRefreshUploadDemo {

    // Specify the AccessKey pair.
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // Specify the region from which you want to access ApsaraVideo VOD.
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * Refresh the credential for uploading an audio file or a video.
     * @param client The client that sends a request.
     * @return RefreshUploadVideoResponse The fields that must be contained in the response.
     * @throws Exception
     */
    public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient client) throws Exception {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        // The ID of the audio file or video.
        request.setVideoId("<VideoId>");
        return client.getAcsResponse(request);
    }

    // The sample code of sending a 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());
        }
    }
}

Obtain a URL and a credential for uploading an image

You can call the CreateUploadImage operation to obtain a URL and a credential for uploading an image.

For more information about the request and response parameters of this operation, see CreateUploadImage. The following sample code provides an example:

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;

/**
 * The sample code of obtaining a URL and a credential for uploading an image.
 *
 */
public class ImageCreateUploadDemo {

    // Specify the AccessKey pair.
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // Specify the region from which you want to access ApsaraVideo VOD.
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * Obtain a URL and a credential for uploading an image.
     * @param client The client that sends a request.
     * @return CreateUploadImageResponse The fields that must be contained in the response.
     * @throws Exception
     */
    public static CreateUploadImageResponse createUploadImage(DefaultAcsClient client) throws Exception {
        CreateUploadImageRequest request = new CreateUploadImageRequest();
        // Configure the image type.
        request.setImageType("default");
        // Configure the file name extension of the image.
        request.setImageExt("gif");
        // Configure the image title.
        request.setTitle("this is a sample");

        // Optional. The user data that consists of user-defined parameters. If you need a callback URL and transparent data transmission, you can configure the user data.
        JSONObject userData = new JSONObject();

        // The callback configurations in the user data.
        // The callback configurations for event notifications. If you specify callback configurations, the specified callback configurations take effect. Otherwise, the global callback configurations take effect.
        JSONObject messageCallback = new JSONObject();
        // Configure a callback URL.
        messageCallback.put("CallbackURL", "http://192.168.0.0/16");
        // Configure the callback type. The default type is 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);
    }

    // The sample code of sending a 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());
        }
    }
}

Obtain a URL and a credential for uploading an auxiliary media asset

You can call the CreateUploadAttachedMedia operation to obtain a URL and a credential for uploading an auxiliary media asset.

For more information about the request and response parameters of this operation, see CreateUploadAttachedMedia. The following sample code provides an example:

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;

/**
 * The sample code of obtaining a URL and a credential for uploading an auxiliary media asset.
 *
 */
public class AttachedMediaCreateUploadDemo {

    // Specify the AccessKey pair.
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // Specify the region from which you want to access ApsaraVideo VOD.
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * Obtain a URL and a credential for uploading an auxiliary media asset, such as a watermark or subtitle file.
     * @param client The client that sends a request.
     * @return CreateUploadAttachedMediaResponse The fields that must be contained in the response.
     * @throws Exception
     */
    public static CreateUploadAttachedMediaResponse createUploadAttachedMedia(DefaultAcsClient client) throws Exception {
        CreateUploadAttachedMediaRequest request = new CreateUploadAttachedMediaRequest();
        // Configure the business type.
        request.setBusinessType("watermark");
        // Configure the file name extension.
        request.setMediaExt("gif");
        // Configure the title.
        request.setTitle("this is a sample");

        // Optional. The user data that consists of user-defined parameters. If you need a callback URL and transparent data transmission, you can configure the user data.
        JSONObject userData = new JSONObject();

        // The callback configurations in the user data.
        // The callback configurations for event notifications. If you specify callback configurations, the specified callback configurations take effect. Otherwise, the global callback configurations take effect.
        JSONObject messageCallback = new JSONObject();
        // Configure a callback URL.
        messageCallback.put("CallbackURL", "http://192.168.0.0/16");
        // Configure the callback type. The default type is 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);
    }
    // The sample code of sending a 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());
        }
    }
}

Upload multiple media files by using the URLs of source files at a time

You can call the UploadMediaByURL operation to upload multiple media files by using the URLs of source files at a time.

For more information about the request and response parameters of this operation, see UploadMediaByURL. The following sample code provides an example:

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;

/**
 * The sample code of uploading multiple media files by using the URLs of source files at a time.
 *
 */
public class AudioOrVideoUploadByUrl {

    // Specify the AccessKey pair.
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // Specify the region from which you want to access ApsaraVideo VOD.
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * Upload multiple media files by using the URLs of source files at a time.
     *
     * @param client The client that sends a request.
     * @return UploadMediaByURLResponse The fields that must be contained in the response.
     * @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");
        // The URLs of source files.
        request.setUploadURLs(encodeUrl);

        // Upload the metadata of the video.
        JSONObject uploadMetadata = new JSONObject();
        // The URLs of the source files that you want to upload. The URL must be included in the value of the UploadURLs parameter.
        uploadMetadata.put("SourceUrl", encodeUrl);
        // The video title.
        uploadMetadata.put("Title", "upload by url sample");

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

        // Optional. The user data that consists of user-defined parameters. If you need a callback URL and transparent data transmission, you can configure the user data.
        JSONObject userData = new JSONObject();

        // The callback configurations in the user data.
        // The callback configurations for event notifications. If you specify callback configurations, the specified callback configurations take effect. Otherwise, the global callback configurations take effect.
        JSONObject messageCallback = new JSONObject();
        // Configure a callback URL.
        messageCallback.put("CallbackURL", "http://192.168.0.0/16");
        // Configure the callback type. The default type is 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);
    }

    // The sample code of sending a 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());
        }
    }

}

Register media assets

You can call the RegisterMedia operation to register media assets.

For more information about the request and response parameters of this operation, see RegisterMedia. The following sample code provides an example:

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.RegisterMediaRequest;
import com.aliyuncs.vod.model.v20170321.RegisterMediaResponse;

/**
 * The sample code of registering media assets.
 *
 */
public class MediaRegisterDemo {

    // Specify the AccessKey pair.
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // Specify the region from which you want to access ApsaraVideo VOD.
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * Register media assets.
     * @param client The client that sends a request.
     * @return RegisterMediaResponse The fields that must be contained in the response.
     * @throws Exception
     */
    public static RegisterMediaResponse registerMedia(DefaultAcsClient client) throws Exception {
        RegisterMediaRequest request = new RegisterMediaRequest();
        JSONArray metaDataArray = new JSONArray();
        JSONObject metaData = new JSONObject();
        // The title.
        metaData.put("Title", "this is a sample");
        // The URL of the source file. The file name in the URL must be unique. If the file name is repeatedly registered, the unique media ID that is associated with the file name is returned. 
        metaData.put("FileURL", "https://192.168.0.0/16.oss-cn-shanghai.aliyuncs.com/vod_sample.mp4");
        // The metadata of the media asset to be registered.
        metaDataArray.add((metaData));
        request.setRegisterMetadatas(metaDataArray.toJSONString());
        return client.getAcsResponse(request);
    }

    // The sample code of sending a request.
    public static void main(String[] argv)  {

        try {
            DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
            RegisterMediaResponse 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());
                    System.out.print("RequestId = " + response.getRequestId() + "\n");
                }
            }
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }

    }

}

Query the information about URL-based upload jobs

You can call the GetURLUploadInfos operation to query the information about URL-based upload jobs.

For more information about the request and response parameters of this operation, see GetURLUploadInfos. The following sample code provides an example:

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;

/**
 * Query the information about URL-based upload jobs.
 */
public class URLUploadInfosGetDemo {
    
    // Specify the AccessKey pair.
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // Specify the region from which you want to access ApsaraVideo VOD.
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * Query the information about URL-based upload jobs.
     *
     * @param client The client that sends a request.
     * @return GetURLUploadInfosResponse The fields that must be contained in the response.
     * @throws Exception
     */
    public static GetURLUploadInfosResponse getURLUploadInfos(DefaultAcsClient client) throws Exception {
        GetURLUploadInfosRequest request = new GetURLUploadInfosRequest();

        // The URLs of video source files. The URLs must be encoded.
        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, ','));
        // The job IDs. You can obtain the job IDs in the response parameter PlayInfo of the GetPlayInfo operation.
        //request.setJobIds("exampleID1,exampleID2");

        return client.getAcsResponse(request);
    }

    // The sample code of sending a 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());
        }
    }
}