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. Attach one of the following system policies as needed. For more information, see Manage permissions for 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 provides a Java example that shows 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 configuring 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 :
* <p>Initialize the account Client with 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 the endpoint, 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.CreateUploadVideoRequest createUploadVideoRequest = new com.aliyun.vod20170321.models.CreateUploadVideoRequest()
.setFileName("aliyun.mp4")
.setTitle("aliyun");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
com.aliyun.vod20170321.models.CreateUploadVideoResponse resp = client.createUploadVideoWithOptions(createUploadVideoRequest, runtime);
com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(resp));
} catch (TeaException error) {
// This is for demonstration purposes only. Handle exceptions carefully and never ignore them directly in production projects.
// Error message
System.out.println(error.getMessage());
// Diagnostic URL
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 carefully and never ignore them directly in production projects.
// Error message
System.out.println(error.getMessage());
// Diagnostic URL
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}