Instal SDK server ApsaraVideo Live untuk Java dan panggil operasi API menggunakan contoh kode.
Prasyarat
-
RAM user yang telah diberi otorisasi untuk memanggil API ApsaraVideo Live. Buat dan berikan otorisasi kepada RAM user.
-
AccessKey pair untuk otentikasi. Buat AccessKey pair.
-
Java 1.8 atau versi lebih baru.
Gunakan SDK
Instal SDK
Tambahkan dependensi Maven berikut ke file pom.xml Anda:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-live20161101</artifactId>
<version>2.0.1</version>
</dependency>
Panggil operasi API
Contoh berikut melakukan kueri terhadap konfigurasi snapshot.
// File ini dihasilkan secara otomatis, jangan mengeditnya. Terima kasih.
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 {
// Konfigurasi HttpClient
/*HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
.connectionTimeout(Duration.ofSeconds(10)) // Atur timeout koneksi. Nilai default: 10 detik.
.responseTimeout(Duration.ofSeconds(10)) // Atur timeout respons. Nilai default: 20 detik.
.maxConnections(128) // Atur ukuran kolam koneksi.
.maxIdleTimeOut(Duration.ofSeconds(50)) // Atur timeout kolam koneksi. Nilai default: 30 detik.
// Konfigurasikan proxy.
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<YOUR-PROXY-HOSTNAME>", 9001))
.setCredentials("<YOUR-PROXY-USERNAME>", "<YOUR-PROXY-PASSWORD>"))
// Untuk koneksi HTTPS, Anda harus mengonfigurasi sertifikat atau mengabaikan sertifikat dengan menggunakan .ignoreSSL(true).
.x509TrustManagers(new X509TrustManager[]{})
.keyManagers(new KeyManager[]{})
.ignoreSSL(false)
.build();*/
// Konfigurasikan kredensial.
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
// Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID dan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah disetel.
.accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
//.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // Gunakan token STS.
.build());
// Konfigurasikan client.
AsyncClient client = AsyncClient.builder()
.region("<Your RegionId>") // ID wilayah
//.httpClient(httpClient) // Gunakan HttpClient yang telah dikonfigurasi. Jika tidak, HttpClient Apache default akan digunakan.
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Konfigurasikan tingkat layanan.
// Ganti konfigurasi tingkat client untuk mengatur parameter seperti titik akhir dan parameter permintaan HTTP.
.overrideConfiguration(
ClientOverrideConfiguration.create()
// Untuk daftar titik akhir, lihat https://api.aliyun.com/product/live.
.setEndpointOverride("live.aliyuncs.com")
//.setConnectTimeout(Duration.ofSeconds(30))
)
.build();
// Atur parameter untuk permintaan API.
DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = DescribeLiveSnapshotConfigRequest.builder()
.domainName("<Your DomainName>")
.appName("<Your AppName>")
// Ganti konfigurasi tingkat permintaan untuk mengatur parameter permintaan HTTP.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
// Dapatkan respons API secara asinkron.
CompletableFuture<DescribeLiveSnapshotConfigResponse> response = client.describeLiveSnapshotConfig(describeLiveSnapshotConfigRequest);
// Dapatkan respons API secara sinkron.
DescribeLiveSnapshotConfigResponse resp = response.get();
System.out.println(new Gson().toJson(resp));
// Proses respons secara asinkron.
/*response.thenAccept(resp -> {
System.out.println(new Gson().toJson(resp));
}).exceptionally(throwable -> { // Tangani pengecualian.
System.out.println(throwable.getMessage());
return null;
});*/
// Tutup client.
client.close();
}
}
-
Ganti nilai placeholder dalam tanda kurung sudut (
<...>) dengan nilai aktual Anda. -
<region ID>: wilayah tempat layanan Anda dideploy. Nilai yang valid: Titik akhir. -
<ALIBABA_CLOUD_ACCESS_KEY_ID>dan<ALIBABA_CLOUD_ACCESS_KEY_SECRET>: Pasangan Kunci Akses Anda. Prasyarat. -
DescribeLiveSnapshotConfigRequest adalah kelas permintaan untuk melakukan kueri terhadap konfigurasi snapshot.
-
DescribeLiveSnapshotConfigResponse adalah kelas respons untuk melakukan kueri terhadap konfigurasi snapshot.
-
Parameter permintaan dan respons: DescribeLiveSnapshotConfig.
-
Pemanggilan API tunduk pada batas QPS. Kontrol alur.
Contoh di atas mendeserialisasi respons menjadi objek DescribeLiveSnapshotConfigResponse. Untuk mendapatkan objek HttpResponse mentah, panggil doAction sebagai gantinya.
HttpResponse httpResponse=client.doAction(describeLiveStreamSnapshotInfoRequest);
int status=httpResponse.getStatus();
Catatan penggunaan:
-
2xx: pemanggilan berhasil.
-
3xx atau 4xx: SDK melemparkan ClientException.
-
5xx: SDK melemparkan ServerException.
Pilih client.getAcsResponse atau client.doAction sesuai kebutuhan Anda.
Konvensi penamaan: [APIName]Request adalah kelas permintaan, dan [APIName]Response adalah kelas respons.
Operasi API umum:
|
API |
Kelas permintaan |
Kelas respons |
|
DescribeLiveSnapshotConfig |
DescribeLiveSnapshotConfig |
|
|
AddLiveAppRecordConfig |
AddLiveAppRecordConfig |
|
|
DescribeLiveRecordConfig |
DescribeLiveRecordConfig |
|
|
AddLiveStreamTranscode |
AddLiveStreamTranscode |
|
|
DescribeLiveStreamTranscodeInfo |
DescribeLiveStreamTranscodeInfo |
|
|
BatchSetLiveDomainConfigs |
BatchSetLiveDomainConfigs |
|
|
DescribeLiveDomainConfigs |
DescribeLiveDomainConfigs |
|
|
Untuk informasi tentang semua operasi API yang tersedia, lihat ikhtisar API. |
||
Kontrol akses
Setiap operasi API mencakup informasi otorisasi. Sebagai contoh, operasi AddLiveAppSnapshotConfig memiliki detail berikut.
Action: live:AddLiveAppSnapshotConfig. Tingkat akses: create. Jenis resource: Domain. Format ARN: acs:cdn:*:{#accountId}:domain/{#DomainName}. Tidak ada kunci kondisi atau operasi terkait.
Untuk kontrol akses yang lebih granular, seperti membatasi RAM user hanya pada domain tertentu, buat kebijakan kustom.
Detail kontrol akses untuk setiap operasi: Otorisasi RAM.