メディアIDに基づいて動画を再生するには、RAM (Resource Access Management) のSTS (Security Token Service) を使用する必要があります。 このトピックでは、セキュリティトークンを要求する方法について説明します。

始める前に

RAMがプログラミング言語用に提供するSTS SDKは、セキュリティトークンを要求する前にインストールされます。 このトピックでは、例としてSTS SDK for Javaを使用します。 STS SDK For Javaのインストール方法とサンプルコードの詳細については、「はじめに」をご参照ください。 For more information about the sample code provided for STS SDKs for other programming languages, see 概要.

このタスクについて

メディアワークフローでは、各入力メディアファイルは、メディアIDによって一意に指定されます。 You can associate multiple output formats and resolutions with a media ID. さまざまな形式のメディアファイルを再生し、解像度を自動的に切り替える場合は、メディアIDに基づいてメディアファイルを再生できます。 メディアIDに基づいてビデオを再生するには、RAMのSTSを使用する必要があります。
セキュリティを確保するには、メディアIDに基づいて暗号化ビデオを再生する必要があります。

準備

セキュリティトークンをリクエストする前に、STSが必要とするロールのAlibaba Cloud Resource Name (ARN) を取得する必要があります。
  1. RAM コンソールにログインします。 左側のナビゲーションウィンドウで、[ID] > [ロール] を選択します。
  2. [ロール] ページで、ARNを取得するロールの名前を [ロール名] 列でクリックします。 ロールの詳細ページが表示されます。
  3. In the Basic Information section, copy the value of the ARN parameter. 後で使用するためにARNの機密を保持します。 arn
  1. Reference STS SDK for Java in the pom.xml file.
    <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>
     </リポジトリ>
    <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>
    aliyun-java-sdk-coreパッケージの最新バージョンは、Mavenリポジトリから取得できます。
  2. セキュリティトークンを生成します。
    // メイン関数。
    public static void main(String[] args) throws Exception {
       IClientProfile profile = DefaultProfile.getProfile(
                                              "cn-hangzhou",
                                              <accessKeyId>,
                                              <accessKeySecret>);
       DefaultAcsClient client = new DefaultAcsClient(profile);
       AssumeRoleResponse response = assumeRole(client, <roleArn>);
       AssumeRoleResponse.Credentials credentials = response.getCredentials();
       System.out.println(credentials.getAccessKeyId() + "\n" +
                          credentials.getAccessKeySecret() + "\n" +
                          credentials.getSecurityToken() + "\n" +
                          credentials.getExpiration();
    }
    // 一時的なAccessKeyペアとセキュリティトークンを生成する関数。
    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. セキュリティトークンの有効期間を調整します。

    The security token generated in the sample code is valid for 900s. 必要に応じて、セキュリティトークンの有効期間を調整できます。 値の範囲: 900秒から3600秒。

    During the validity period of a security token, you can use the security token without the need to generate a new one. 次のコードを使用して、新しいセキュリティトークンを生成する必要があるかどうかを確認できます。
    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;
         }
    }