全部產品
Search
文件中心

ApsaraVideo Media Processing:請求安全性權杖

更新時間:Jun 30, 2024

使用媒體工作流程時,每個多媒體輸入檔案由媒體ID統一標識,一一對應。媒體ID可以關聯多個格式、多個清晰度的輸出,當您需要實現多清晰度自動切換、多格式支援播放時,可以使用媒體ID播放視頻。ApsaraVideo for Media Processing中為保證加密視頻的安全性,加密視頻的播放必須使用媒體ID的播放方式。通過媒體ID播放視頻的過程中,為了確保訪問的安全性和許可權控制的有效性,需要藉助存取控制RAM服務產生並使用安全性權杖。本文為您介紹如何獲得安全性權杖。

前提條件

在請求安全性權杖之前,您需要安裝存取控制(RAM)服務提供的STS SDK。本文以Java為例,詳細操作及程式碼範例請參見Java樣本。如需其他語言程式碼範例,請參見STS SDK概覽

使用前準備

您在請求之前需要先準備好STS需要的角色的參數roleArn

  1. 登入 RAM 控制台,單擊 身份管理 > 角色

  2. 在角色列表中找到具體 角色名稱 後,單擊角色名稱進入基本資料頁面。

  3. 複製ARN參數並妥善儲存,供後續使用。arn

操作步驟

  1. 在pom.xml檔案中引用STS SDK。

    <repositories>
         <repository>
             <id>sonatype-nexus-staging</id>
             <name>Sonatype Nexus Staging</name>
             <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
             <releases>
                 <enabled>true</enabled>
             </releases>
             <snapshots>
                 <enabled>true</enabled>
             </snapshots>
         </repository>
     </repositories>
    <dependencies>
     <dependency>
       <groupId>com.aliyun</groupId>
       <artifactId>aliyun-java-sdk-sts</artifactId>
       <version>2.1.6</version>
     </dependency>
     <dependency>
       <groupId>com.aliyun</groupId>
       <artifactId>aliyun-java-sdk-core</artifactId>
       <version>2.2.0</version>
     </dependency>
    </dependencies>

    說明

    請訪問Maven倉庫擷取aliyun-java-sdk-core的最新版本。

  2. 產生令牌。

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.http.MethodType;
    import com.aliyuncs.http.ProtocolType;
    import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
    import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
    
    /**
     * 此介面僅支援通過RAM帳號調用
     */
    public class STSTokenDemo {
    
        public static void main(String[] args) throws Exception {
            // 初始化Client。
            DefaultAcsClient client = InitClient.initMpsClient();
            // RoleArn 需要在 RAM 控制台上擷取
            String roleArn = "acs:ram::174*********11242:role/sts-role";
    
            try {
                AssumeRoleResponse response = assumeRole(client, roleArn);
                System.out.println("Expiration: " + response.getCredentials().getExpiration());
                System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId());
                System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
                System.out.println("Security Token: " + response.getCredentials().getSecurityToken());
                System.out.println("RequestId: " + response.getRequestId());
    
            } catch (ClientException e) {
                System.out.println("Failed to get a token.");
                System.out.println("Error code: " + e.getErrCode());
                System.out.println("Error message: " + e.getErrMsg());
            }
        }
        /**產生臨時AK和Token的函數*/
        private static AssumeRoleResponse assumeRole(
                DefaultAcsClient client,
                String roleArn)
                throws ClientException {
            final AssumeRoleRequest request = new AssumeRoleRequest();
            request.setVersion("2015-04-01");
            request.setMethod(MethodType.POST);
            request.setProtocol(ProtocolType.HTTPS);
            request.setDurationSeconds(900L);
            request.setRoleArn(roleArn);
            request.setRoleSessionName("test-token");
            return client.getAcsResponse(request);
        }
    }
  3. 調整Token有效期間。

    範例程式碼中產生的Token有效時間為900秒,可以根據實際需求調整(最小900秒,最大3,600秒)。

    在有效期間內,不需要反覆產生新的Token,可以複用已經產生的Token。您可以通過以下方式判斷何時需要重建Token:

    private static boolean isTimeExpire(String expiration) {
         Date nowDate = new Date();
         Date expireDate = javax.xml.bind.DatatypeConverter.parseDateTime(expiration).getTime();
         if (expireDate.getTime() <= nowDate.getTime()) {
             return true;
         } else {
             return false;
         }
    }

相關文檔