すべてのプロダクト
Search
ドキュメントセンター

ApsaraVideo Live:Java SDK の使用

最終更新日:Jan 30, 2026

このトピックでは、ApsaraVideo Live サーバーサイド用 Java SDK の使用方法とサンプルコードを紹介します。

前提条件

  • Resource Access Management (RAM) ユーザーを作成し、ApsaraVideo Live API にアクセスするために必要な権限を付与します。権限付与の詳細については、「RAM ユーザーの作成と権限付与」をご参照ください。

  • AccessKey ペアを取得します。AccessKey ペアは、サーバーサイド API を呼び出す際の本人確認に使用されます。RAM ユーザー用の AccessKey ペアの作成方法については、「AccessKey ペアの作成」をご参照ください。

  • Java 環境をセットアップします。Java 1.8 以降が必要です。

SDK の使用

SDK のインストール

Maven を使用して SDK をインストールするには、プロジェクトの pom.xml ファイルに以下の依存関係を追加します。

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>alibabacloud-live20161101</artifactId>
  <version>2.0.1</version>
</dependency>

使用方法

以下のサンプルコードは、ライブストリームのスナップショット構成を照会する方法を示しています。

// This file is auto-generated, don't edit it. Thanks.
package demo;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.live20161101.models.*;
import com.aliyun.sdk.service.live20161101.*;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;

//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.io.*;

public class DescribeLiveSnapshotConfig {
    public static void main(String[] args) throws Exception {

        // HttpClient Configuration
        /*HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
                .connectionTimeout(Duration.ofSeconds(10)) // Set the connection timeout time, the default is 10 seconds
                .responseTimeout(Duration.ofSeconds(10)) // Set the response timeout time, the default is 20 seconds
                .maxConnections(128) // Set the connection pool size
                .maxIdleTimeOut(Duration.ofSeconds(50)) // Set the connection pool timeout, the default is 30 seconds
                // Configure the proxy
                .proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<YOUR-PROXY-HOSTNAME>", 9001))
                        .setCredentials("<YOUR-PROXY-USERNAME>", "<YOUR-PROXY-PASSWORD>"))
                // If it is an https connection, you need to configure the certificate, or ignore the certificate(.ignoreSSL(true))
                .x509TrustManagers(new X509TrustManager[]{})
                .keyManagers(new KeyManager[]{})
                .ignoreSSL(false)
                .build();*/

        // Configure Credentials authentication information, including ak, secret, token
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
                .accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                .accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("<Your RegionId>") // Region ID
                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Service-level configuration
                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                  // For information about endpoints, see https://api.aliyun.com/product/live
                                .setEndpointOverride("live.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();

        // Parameter settings for API request
        DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = DescribeLiveSnapshotConfigRequest.builder()
                .domainName("<Your DomainName>")
                .appName("<Your AppName>")
                // Request-level configuration rewrite, can set Http request parameters, etc.
                // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                .build();

        // Asynchronously get the return value of the API request
        CompletableFuture<DescribeLiveSnapshotConfigResponse> response = client.describeLiveSnapshotConfig(describeLiveSnapshotConfigRequest);
        // Synchronously get the return value of the API request
        DescribeLiveSnapshotConfigResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        // Asynchronous processing of return values
        /*response.thenAccept(resp -> {
            System.out.println(new Gson().toJson(resp));
        }).exceptionally(throwable -> { // Handling exceptions
            System.out.println(throwable.getMessage());
            return null;
        });*/

        // Finally, close the client
        client.close();
    }

}
説明
  • このサンプルコードはテスト目的で使用できます。角括弧(<>)内の値は、実際の値に置き換えてください。

  • <regionId> はリージョン ID です。リージョン ID の取得方法については、「エンドポイント」をご参照ください。

  • <ALIBABA_CLOUD_ACCESS_KEY_ID> および <ALIBABA_CLOUD_ACCESS_KEY_SECRET> は、ご利用の AccessKey ID および AccessKey Secret です。AccessKey ペアをお持ちでない場合は、本トピックの「前提条件」セクションをご確認ください。

  • DescribeLiveSnapshotConfigRequest は、スナップショット構成を照会するためのリクエストクラスです。

  • DescribeLiveSnapshotConfigResponse は、スナップショット構成を照会するためのレスポンスクラスです。

  • リクエストパラメーターおよびレスポンスパラメーターの詳細については、「DescribeLiveSnapshotConfig」API リファレンスをご参照ください。

  • API 呼び出しには、クエリ/秒(QPS)制限が適用されます。QPS 制限の詳細については、「スロットリング」をご参照ください。

上記の例では、DescribeLiveSnapshotConfigResponse は逆シリアル化されたレスポンスオブジェクトです。逆シリアル化せずに直接 HttpResponse の結果を取得するには、呼び出すメソッドを変更します。

HttpResponse httpResponse=client.doAction(describeLiveStreamSnapshotInfoRequest);
int status=httpResponse.getStatus();

注意事項:

  • HTTP ステータスコードが 200 ~ 299 の場合、API 呼び出しが成功したことを示します。

  • HTTP ステータスコードが 300 ~ 499 の場合、クライアントエラーが発生しています。サーバーサイド SDK は ClientException をスローします。

  • HTTP ステータスコードが 500 以上の場合、サーバーサイドエラーが発生しています。サーバーサイド SDK は ServerException をスローします。

必要に応じて、client.getAcsResponse メソッドまたは client.doAction メソッドのいずれかを使用できます。

重要

ApsaraVideo Live SDK を使用する際は、以下の命名規則に従ってください。API のリクエストクラス名は APIRequest、レスポンスクラス名は APIResponse となります。

以下に一般的な API の一覧を示します。

API

リクエストクラス

レスポンスクラス

ドメイン名のスクリーンショット構成の照会

DescribeLiveSnapshotConfigRequest

DescribeLiveSnapshotConfigResponse

録画構成の追加

AddLiveAppRecordConfigRequest

AddLiveAppRecordConfigResponse

ドメイン名に対するすべてのアプリ録画構成の照会

DescribeLiveRecordConfigRequest

DescribeLiveRecordConfigResponse

トランスコード構成の追加

AddLiveStreamTranscodeRequest

AddLiveStreamTranscodeResponse

トランスコード構成の照会

DescribeLiveStreamTranscodeInfoRequest

DescribeLiveStreamTranscodeInfoResponse

ドメイン名の一括構成

BatchSetLiveDomainConfigsRequest

BatchSetLiveDomainConfigsResponse

ライブストリーミングドメイン名の構成の照会

DescribeLiveDomainConfigsRequest

DescribeLiveDomainConfigsResponse

API の詳細については、「API 概要」をご参照ください。

リソース制御

API ドキュメントを確認すると、権限付与情報が記載されていることに気付くかもしれません。たとえば、「AddLiveAppSnapshotConfig」API リファレンスには、以下の権限付与情報が含まれています。

image

RAM ユーザーに権限を付与すると、そのユーザーが API を呼び出せるようになります。ただし、場合によってはより細かい粒度でのアクセスの制御が必要になることがあります。たとえば、同じ API を呼び出す際に、RAM ユーザー A には DomainA のみ、RAM ユーザー B には DomainB のみを操作できるようにしたい場合があります。このような要件を満たすには、リソースベースの権限付与を使用します。詳細については、「カスタムポリシーの作成」をご参照ください。

説明

各 API がアクセス制御に対応しているリソースの詳細については、「API 権限付与の概要」をご参照ください。