當您使用上傳地址和憑證方式上傳媒體資源時,需要在應用服務端部署授權服務,從點播服務擷取上傳地址和憑證給用戶端使用。
背景資訊
您需要提前瞭解使用上傳地址和憑證的整體上傳流程。
擷取上傳地址和憑證
您的應用伺服器需要根據不同的媒體類型調用不同的OpenAPI,擷取上傳地址和憑證。
提供擷取音視頻上傳地址和憑證的介面:服務端調用CreateUploadVideo - 擷取音視頻上傳地址和憑證。
提供重新整理音視頻上傳地址和憑證的介面:服務端調用重新整理視頻上傳憑證。
提供擷取圖片上傳地址和憑證的介面:服務端調用CreateUploadImage - 擷取圖片上傳地址和憑證。
提供擷取輔助媒資上傳地址和憑證的介面:服務端調用CreateUploadAttachedMedia - 擷取輔助媒資上傳地址和憑證。
步驟一:準備RAM使用者
建立RAM使用者,注意以下幾點:
設定允許使用永久AccessKey訪問。
妥善保管擷取的AccessKey。
步驟二:為RAM使用者授權
為RAM使用者授予調用對應OpenAPI的許可權。您可以根據需要選擇以下系統策略。詳細授權操作請參見為RAM使用者授權。
策略名稱稱 | 說明 |
AliyunVodFullAccess | 管理ApsaraVideo for VOD服務(VOD)的許可權 |
AliyunVodUploadAuth | ApsaraVideo for VOD服務(VOD)上傳許可權 |
步驟三:調用OpenAPI
本文以Java程式設計語言擷取視頻資源的上傳地址和憑證為例進行介紹。請提前在系統內容中配置AccessKey。若要給用戶端提供API介面,請對範例程式碼進行改造。
V1.0 SDK
在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());
}
}
}
V2.0 SDK
在Maven中添加如下依賴資訊:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>vod20170321</artifactId>
<version>3.6.4</version>
</dependency>說明
工程代碼建議使用更安全的無AK方式,憑據配置方式請參見管理訪問憑據。
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* <b>description</b> :
* <p>使用憑據初始化帳號Client</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);
// Endpoint 請參考 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) {
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}