Use the server SDK for Java

Updated at:
Copy as MD

Install the ApsaraVideo Live server SDK for Java and call API operations with code examples.

Prerequisites

Use the SDK

Install the SDK

Add the following Maven dependency to your pom.xml file:

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

Call an API operation

The following example queries a snapshot configuration.

// 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. Default value: 10 seconds.
                .responseTimeout(Duration.ofSeconds(10)) // Set the response timeout. Default value: 20 seconds.
                .maxConnections(128) // Set the connection pool size.
                .maxIdleTimeOut(Duration.ofSeconds(50)) // Set the connection pool timeout. Default value: 30 seconds.
                // Configure the proxy.
                .proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<YOUR-PROXY-HOSTNAME>", 9001))
                        .setCredentials("<YOUR-PROXY-USERNAME>", "<YOUR-PROXY-PASSWORD>"))
                // For an HTTPS connection, you must configure a certificate or ignore the certificate by using .ignoreSSL(true).
                .x509TrustManagers(new X509TrustManager[]{})
                .keyManagers(new KeyManager[]{})
                .ignoreSSL(false)
                .build();*/
        // Configure credentials.
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                // Ensure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables 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 an STS token.
                .build());
        // Configure the client.
        AsyncClient client = AsyncClient.builder()
                .region("<Your RegionId>") // region ID
                //.httpClient(httpClient) // Use the configured HttpClient. Otherwise, the default Apache HttpClient is used.
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Configure the service level.
                // Override the client-level configuration to set parameters such as the endpoint and HTTP request parameters.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                  // For a list of endpoints, see https://api.aliyun.com/product/live.
                                .setEndpointOverride("live.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();
        // Set the parameters for the API request.
        DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = DescribeLiveSnapshotConfigRequest.builder()
                .domainName("<Your DomainName>")
                .appName("<Your AppName>")
                // Override the request-level configuration to set HTTP request parameters.
                // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                .build();
        // Asynchronously get the API response.
        CompletableFuture<DescribeLiveSnapshotConfigResponse> response = client.describeLiveSnapshotConfig(describeLiveSnapshotConfigRequest);
        // Synchronously get the API response.
        DescribeLiveSnapshotConfigResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        // Asynchronously process the response.
        /*response.thenAccept(resp -> {
            System.out.println(new Gson().toJson(resp));
        }).exceptionally(throwable -> { // Handle exceptions.
            System.out.println(throwable.getMessage());
            return null;
        });*/
        // Close the client.
        client.close();
    }
}
Note
  • Replace placeholder values in angle brackets (<...>) with your actual values.

  • <region ID>: the region where your service is deployed. Valid values: Endpoints.

  • <ALIBABA_CLOUD_ACCESS_KEY_ID> and <ALIBABA_CLOUD_ACCESS_KEY_SECRET>: your AccessKey pair. Prerequisites.

  • DescribeLiveSnapshotConfigRequest is the request class for querying a snapshot configuration.

  • DescribeLiveSnapshotConfigResponse is the response class for querying a snapshot configuration.

  • Request and response parameters: DescribeLiveSnapshotConfig.

  • API calls are subject to QPS limits. Flow control.

The preceding example deserializes the response into a DescribeLiveSnapshotConfigResponse object. To get the raw HttpResponse object, call doAction instead.

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

Usage notes:

  • 2xx: the call succeeded.

  • 3xx or 4xx: the SDK throws a ClientException.

  • 5xx: the SDK throws a ServerException.

Choose client.getAcsResponse or client.doAction based on your requirements.

Important

Naming convention: [APIName]Request is the request class, and [APIName]Response is the response class.

Common API operations:

API

Request class

Response class

DescribeLiveSnapshotConfig

DescribeLiveSnapshotConfigRequest

DescribeLiveSnapshotConfigResponse

AddLiveAppRecordConfig

AddLiveAppRecordConfigRequest

AddLiveAppRecordConfigResponse

DescribeLiveRecordConfig

DescribeLiveRecordConfigRequest

DescribeLiveRecordConfigResponse

AddLiveStreamTranscode

AddLiveStreamTranscodeRequest

AddLiveStreamTranscodeResponse

DescribeLiveStreamTranscodeInfo

DescribeLiveStreamTranscodeInfoRequest

DescribeLiveStreamTranscodeInfoResponse

BatchSetLiveDomainConfigs

BatchSetLiveDomainConfigsRequest

BatchSetLiveDomainConfigsResponse

DescribeLiveDomainConfigs

DescribeLiveDomainConfigsRequest

DescribeLiveDomainConfigsResponse

For information about all available API operations, see the API overview.

Access control

Each API operation includes authorization information. For example, the AddLiveAppSnapshotConfig operation has the following details.

Action: live:AddLiveAppSnapshotConfig. Access level: create. Resource type: Domain. ARN format: acs:cdn:*:{#accountId}:domain/{#DomainName}. No condition keys or associated operations.

For more granular access control, such as restricting a RAM user to a specific domain, create a custom policy.

Note

Access control details for each operation: RAM authorization.