To upload media resources using an upload URL and credential, you need to deploy an authorization service on your application server. This service obtains the upload URL and credential from ApsaraVideo VOD and provides them to your client.
Background information
Before you start, familiarize yourself with the overall upload process that uses an upload URL and credential.
Obtain an upload URL and credential
Your application server must call different OpenAPI operations to obtain upload URLs and credentials for different media types.
To obtain the upload URL and credential for an audio or video file, call the CreateUploadVideo operation.
To refresh the upload credential for an audio or video file, call the RefreshUploadVideo operation.
To obtain the upload URL and credential for an image, call the CreateUploadImage operation.
To obtain the upload URL and credential for an auxiliary media asset, call the CreateUploadAttachedMedia operation.
Step 1: Prepare a RAM user
Create a Resource Access Management (RAM) user and note the following:
Enable Programmatic Access.
Securely store the AccessKey pair that you obtain.
Step 2: Grant permissions to the RAM user
Grant the RAM user permissions to call the required OpenAPI operations. You can attach one of the following system policies to the RAM user as needed. For more information, see Grant permissions to a RAM user.
Policy Name | Description |
AliyunVodFullAccess | Permissions to manage ApsaraVideo VOD. |
AliyunVodUploadAuth | Permissions to upload to ApsaraVideo VOD. |
Step 3: Call an OpenAPI operation
This topic uses a Java example to demonstrate how to obtain the upload URL and credential for a video resource. Before you start, configure an AccessKey pair in your system environment. You can modify the sample code as needed to provide an API operation for your client.
SDK V1.0
Add the following dependencies in Maven:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.7.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>2.16.32</version>
</dependency>import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.vod.model.v20170321.*;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.0</version>
</dependency>
*/
public class CreateUploadVideo {
public static void main(String[] args) {
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
IAcsClient client = new DefaultAcsClient(profile);
CreateUploadVideoRequest request = new CreateUploadVideoRequest();
request.setFileName("aliyun.mp4");
request.setTitle("aliyun");
try {
CreateUploadVideoResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
SDK V2.0
Add the following dependency in Maven:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>vod20170321</artifactId>
<version>3.6.4</version>
</dependency>For enhanced security, use a credential-free method in your project code. For more information about how to configure credentials, see Manage access credentials.
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description