Batch upload from URLs

Updated at:
Copy as MD

If your files are not stored locally but are accessible from public URLs, you can use ApsaraVideo VOD to pull and upload files directly from public URLs. This method eliminates the need to download files to a local machine, making it ideal for offline site migration scenarios.

Usage notes

  • The batch upload by pulling from URLs is an asynchronous task. After you submit a task, it may take several hours or even days to complete. If timeliness is critical, use the upload SDK or the OSS SDK.

  • ApsaraVideo VOD creates a new media asset with a new media ID each time you submit an upload task for the same URL.

  • Batch upload by pulling from URLs supports only video and audio formats. For more information, see Supported media formats.

  • If a single file is larger than 20 GB, the upload will fail. Use a different upload method.

Upload using the console

In the console, you can upload audio and video files by using the From URL method in the following regions: China (Shanghai), China (Beijing), China (Shenzhen), Singapore, and US (Silicon Valley). This upload method is not supported in other regions.

  1. Log on to the ApsaraVideo VOD console.

  2. In the left-side navigation pane, under Media Files, click Audio/Video .

  3. Click Upload .

  4. Click Add Media.

  5. Add files to upload, configure transcoding, and click Upload.

    When you choose the From URL method, if a URL does not contain an extension, specify it to improve the success rate. Pull tasks may fail due to network issues or the resource's service region. If a task fails, adjust your network settings or the resource's service region, and then try again.

    In the table, enter the URL, extension, and Audio/Video Name. By default, Category is set to Uncategorized and Transcoding Template Group is set to No Transcoding. You can click + Add to add more files.

Upload using an API

To submit tasks by API, integrate the ApsaraVideo VOD server-side SDK and call the UploadMediaByURL operation. This operation is supported only in the following regions: China (Shanghai), China (Beijing), China (Shenzhen), Singapore, and US (Silicon Valley).

Prerequisites

  • ApsaraVideo VOD is activated. For more information, see Activate ApsaraVideo VOD.

  • The system settings required for the upload, including the storage path in the specified region and the callback settings, are configured. For more information, see Manage storage buckets and Configure callbacks.

  • A RAM user is created and used to access ApsaraVideo VOD. To prevent security risks caused by the leakage of the AccessKey pair of your Alibaba Cloud account, we recommend that you create a RAM user and grant the RAM user the permissions required to access ApsaraVideo VOD. Then, you can use the AccessKey pair of the RAM user to access ApsaraVideo VOD. For more information, see Create a RAM user and grant permissions.

  • Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. For more information, see Configure environment variables in Linux, macOS, and Windows.

    Important
    • An Alibaba Cloud account AccessKey pair has permissions on all API operations. Use a RAM user's AccessKey pair to call API operations or perform routine O&M.

    • Do not hard-code your AccessKey ID and AccessKey secret in project code. If leaked, all resources in your account are compromised.

  • Optional. A role is created for the RAM user and the role is granted the permissions required to access ApsaraVideo VOD if you want to access ApsaraVideo VOD by using Security Token Service (STS). For more information, see Obtain an STS token.

Sample code

For sample code in other languages, see the SDK Example section of UploadMediaByURL.

V1.0 SDK

Step 1: Install dependencies

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.6.1</version>
</dependency>
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-vod</artifactId>
  <version>2.16.32</version>
</dependency>

Step 2: Sample code

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
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;
/**
 * Sample code for the batch upload by pulling from URLs feature.
 *
 */
public class AudioOrVideoUploadByUrl {
    /** 
     * Initializes the ApsaraVideo VOD client.
     */
    public static DefaultAcsClient initVodClient() throws ClientException {
    // The region where ApsaraVideo VOD is activated.
    String regionId = "cn-shanghai"; 
    // The AccessKey pair of an Alibaba Cloud account has permissions to access all API operations. We recommend that you use the AccessKey pair of a RAM user for API calls or routine O&M.
    // We strongly recommend that you do not hard-code the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, which compromises the security of all resources in your account.
    // This example shows how to use an AccessKey pair that is obtained from environment variables to authenticate your identity for API access. Before you run the sample code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
    DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    DefaultAcsClient client = new DefaultAcsClient(profile);
    return client;
    }
    /**
     * Uploads media files by pulling from URLs in batches.
     *
     * @param client The client that sends requests.
     * @return UploadMediaByURLResponse The response to the batch upload request.
     * @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 URL of the source video file.
        request.setUploadURLs(encodeUrl);
        //The metadata of the video to upload.
        JSONObject uploadMetadata = new JSONObject();
        //The URL of the source video file to upload. This URL must match a URL in UploadURLs to take effect.
        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());
        //UserData: Custom parameter settings. Set this parameter if you need a separate callback URL and pass-through data. This parameter is optional.
        JSONObject userData = new JSONObject();
        //Callback settings in UserData.
        //Message callback settings. If specified, these settings are used. Otherwise, the global event notification settings are used.
        JSONObject messageCallback = new JSONObject();
        //The callback URL.
        messageCallback.put("CallbackURL", "http://192.168.0.0/16");
        //The callback type. Default value: 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);
    }
    /** 
     * Sample request.
     */
    public static void main(String[] argv) {
        try {
            DefaultAcsClient client = initVodClient();
            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());
        }
    }
}

V2.0 SDK

Step 1: Install dependencies

    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>vod20170321</artifactId>
      <version>3.6.4</version>
    </dependency>

Step 2: Sample code

Note

We recommend that you use a more secure, AK-free method for your project code. For information about how to configure credentials, see Manage access credentials.

package com.aliyun.sample;
import com.aliyun.tea.*;
public class UploadMediaByURL {
    /**
     * <b>description</b> :
     * <p>Initializes the client by using credentials.</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.vod20170321.Client createClient() throws Exception {
        com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setCredential(credential);
        // For more information about endpoints, see https://api.aliyun.com/product/vod.
        config.endpoint = "vod.cn-shanghai.aliyuncs.com";
        return new com.aliyun.vod20170321.Client(config);
    }
    public static void main(String[] args_) throws Exception {
        com.aliyun.vod20170321.Client client = Sample.createClient();
        com.aliyun.vod20170321.models.UploadMediaByURLRequest uploadMediaByURLRequest = new com.aliyun.vod20170321.models.UploadMediaByURLRequest()
                .setUploadURLs("http://cloud.video.taobao.com/play/u/3897629815/p/1/e/6/t/1/301659704826.mp4");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // Print the API response after you copy and run the code.
            com.aliyun.vod20170321.models.UploadMediaByURLResponse response = client.uploadMediaByURLWithOptions(uploadMediaByURLRequest, runtime);
            com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(response));
        } catch (TeaException error) {
            // This is for demonstration purposes only. Handle exceptions with care. Do not ignore exceptions in your project.
            // Error message
            System.out.println(error.getMessage());
            // Diagnostic address
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // This is for demonstration purposes only. Handle exceptions with care. Do not ignore exceptions in your project.
            // Error message
            System.out.println(error.getMessage());
            // Diagnostic address
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}