このトピックでは、Security Token Service (STS) トークンを取得する方法について説明します。 これにより、クライアントアップロードSDKを使用してファイルをアップロードするときのアップロードインスタンスの初期化に備えます。

このタスクについて

When media files are uploaded from clients, the files are directly uploaded to Object Storage Service (OSS) buckets allocated by ApsaraVideo VOD without the need to pass through servers. したがって、クライアントを認証する必要があります。 アップロードURLと資格情報を取得するには、AppServerに認証サービスを展開する必要があります。 クライアントのアップロードSDKは、次の権限付与方法をサポートします。
  • Use an upload URL and credential.
  • STSトークンを使用します。

STSは、Alibaba Cloudサービスへのアクセスを認証するためのユニバーサルサービスを提供します。 STSトークンを使用してメディアファイルをアップロードするクライアントSDKは、すべてのアップロードロジックをカプセル化します。 You need to only focus on the configurations for obtaining the STS token, updating the STS token when it expires, and setting the callback for upload completion. アップロードURLと資格情報またはSTSトークンを使用してメディアファイルをアップロードするかどうかの詳細については、「資格情報とSTSの比較」をご参照ください。 アップロードURLと資格情報を使用してメディアファイルをアップロードする方法の詳細については、「アップロードURLと資格情報の取得」をご参照ください。

プロセス

STSトークンを使用してメディアファイルをアップロードする方法の詳細については、「プロセスのアップロード」をご参照ください。

STSトークンの取得

署名プロセスをスキップするには、STS SDKを統合し、 AssumeRole操作を呼び出してSTSトークンを取得することを推奨します。 STS SDKを統合する前に、RAMユーザーを作成し、ApsaraVideo VODにアクセスする権限を持つロールをユーザーに割り当てる必要があります。

  1. RAM ユーザーを作成します。 詳細については、「ロールを作成し、STSを使用してロールに一時的なアクセス許可を付与する」をご参照ください。
  2. 任意です。 カスタム権限付与ポリシーをRAMユーザーにアタッチします。
  3. STS SDKを統合し、 AssumeRole操作を呼び出してSTSトークンを取得します。 このステップのサブステップは、サーバのプログラミング言語に基づいて変化する。
    サーバーのプログラミング言语操作ガイド
    Javaはじめに
    説明 次のセクションでは、Javaのサンプルコードを示します。
    Pythonインストール
    PHPSDK のダウンロード
    .NETSDK のダウンロード
    Node.jsインストール

Javaのサンプルコード

STSトークンの取得方法に関するサンプルJavaコード

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;


/**
 * @ 著者ジャック
 * @date 2020/5/25
 */
public class TestStsService {

    public static void main(String[] args) {
        // AssumeRole操作を呼び出すことができるのはRAMユーザーのみです。
        // Alibaba CloudアカウントのAccessKeyペアを使用してAssumeRoleリクエストを開始することはできません。
        // Create a RAM user in the Resource Access Management (RAM) console and create an AccessKey pair for the RAM user.
        文字列accessKeyId = "LTAI****";
        String accessKeySecret = "D47l****";
        // Request parameters for the AssumeRole operation include RoleArn, RoleSessionName, Policy, and DurationSeconds.
        // RoleArn: You can obtain the value of this parameter in the RAM console.
        String roleArn = "acs:ram::174809843091****:role/vodrole";
        // RoleSessionName: ロールのセッション名。 You can set this parameter based on your needs.
        String roleSessionName = "session-name";// セッション名を指定します。
        // Specify a policy.
        String policy = "{\n" +
                "  \"Version\": \"1\",\n" +
                " \" ステートメント \": [\n" +
                " {\n" +
                " \" アクション \": \" vod:*\",\n" +
                " \" リソース \": \" *\",\n" +
                "      \"Effect\": \"Allow\"\n" +
                "    }\n" +
                " ]\n" +
                "}";
        try {
            AssumeRoleResponse response = assumeRole(accessKeyId, accessKeySecret, roleArn, roleSessionName, policy);
            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());

            createUploadVideo(response.getCredentials().getAccessKeyId() 、response.getCredentials().getAccessKeySecret() 、response.getCredentials().getSecurityToken());
        } 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());
        }
    }

    静的AssumeRoleResponse assumeRole(String accessKeyId、String accessKeySecret、String roleArn、String roleSessionName、Stringポリシー) はClientException {
        try {
            // デフォルトのプロファイルを作成します。 パラメータは空のままにします。 regionIdパラメーターは不要です。
            /*
            注: SysEndpoint t o sts.aliyuncs.comを設定した場合、regionIdパラメーターはオプションです。 それ以外の場合は、regionIdパラメーターをSTSを使用するリージョンに設定する必要があります。 例: cn-shanghai.
            さまざまなリージョンのSTSエンドポイントの詳細については、「エンドポイント」をご参照ください。 
             */
            IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret);
            // Use the profile to construct a client.
            DefaultAcsClient client = new DefaultAcsClient(profile);
            // AssumeRoleリクエストを作成し、リクエストパラメーターを設定します。
            final AssumeRoleRequest request = new AssumeRoleRequest();
            request.setSysEndpoint("sts.aliyuncs.com");
            request.setSysMethod(MethodType.POST);
            request.setRoleArn(roleArn);
            request.setRoleSessionName(roleSessionName);
            request.setPolicy(policy);
            // Initiate the request and obtain the response.
            final AssumeRoleResponse response = client.getAcsResponse(request);
            return response;
        } catch (ClientException e) {
            throw e;
        }
    }
                

Parameters

パラメーター説明
RoleArn引き受けられるロールのAlibaba Cloudリソース名 (ARN) 。 RAMユーザーのロールを作成したら、次の手順を実行してロールのARNを表示できます。RAMコンソールにログインし、[ID] > [ロール] を選択します。 必要なロールをクリックし、[基本情報] セクションでARNを表示します。
RoleSessionNameロールセッションのカスタム名。 Set this parameter based on your business requirements. ほとんどの場合、このパラメーターは、操作を呼び出すユーザーのID (ユーザー名など) に設定されます。 In ActionTrail logs, you can distinguish the users who assume the same RAM role to perform operations based on the value of the RoleSessionName parameter. これにより、ユーザー固有の監査を実行できます。 値は2 ~ 64文字で、英数字、ピリオド (.) 、アットサイン (@) 、ハイフン (-) 、アンダースコア (_) を使用できます。
Policyロールが引き受けられるときに追加される権限を指定するポリシー。
説明
  • ポリシーは、ユーザーがロールを引き受けた後、一時的なアクセス資格情報の権限を制御するために使用されます。 一時的なアクセス資格情報によって取得される最終的な権限は、ロールの権限とポリシーで指定された権限の共通部分です。
  • The Policy parameter is passed in to improve flexibility. たとえば、このパラメーターを設定して、CreateUploadVideo操作のみを呼び出すことができます。
DurationSeconds一時的なアクセス資格情報の有効期間。 有効値:900 〜 3600。 (単位:秒)
accessKeyId and accessKeySecretロールを引き受けるRAMユーザーのAccessKey IDとAccessKeyシークレット。

STSトークンを使用したメディアファイルのアップロード

各メディアファイルにはSTSトークンが必要です。 Therefore, you must obtain the STS token from the AppServer and specify the STS token for the upload instance in the onUploadStarted callback. The specific settings vary based on different clients.

クライアントユーザーガイド
WebUpload SDK for JavaScript
Androidファイルのアップロード
iOSファイルのアップロード
WeChat mini programWeChat miniプログラム用SDKのアップロード