Bu konu, Alibaba Cloud hizmetlerinin SDK'lerini kullanarak API işlemlerini nasıl çağıracağınızı açıklar.
Alibaba Cloud SDK'leri
Alibaba Cloud; Java, C#, Go, Python, TypeScript + Node.js, PHP ve C++ dahil olmak üzere birden fazla programlama dilinde SDK'ler sunar. Uygulamalarınıza entegre edilen SDK'ler aracılığıyla API işlemlerini doğrudan çağırabilirsiniz. SDK'ler; imzalama mantığı, zaman aşımı mekanizması ve yeniden deneme mekanizması gibi bilgileri kapsüller ve belirtimlere göre yapılandırılmış yanıt nesneleri döndürerek geliştiricilere kolaylık sağlar.
Alibaba Cloud SDK'leri hakkında daha fazla bilgi için bkz. Alibaba Cloud SDK'leri.
Entegrasyon yöntemi
OpenAPI Explorer adresine gidin. Üst gezinme çubuğundaki
simgesinin üzerine gelin ve gereken hizmeti bulun.SDK Examples bölümünde, kullandığınız programlama dilini tıklayın.
SDK'yi entegre etmek için SDK kurulum yöntemini ve örnek kodu görüntüleyin.
V2.0 ve V1.0 Karşılaştırması
Alibaba Cloud SDK V2.0, Alibaba Cloud SDK V1.0'a kıyasla daha fazla dili ve daha karmaşık API işlemi senaryolarını destekler. V2.0 hem eşzamansız hem de eşzamanlı çağrıları destekler ve V1.0'daki bazı eski sorunları çözmüştür. V2.0 daha esnek ve güçlü özellikler sunar. Alibaba Cloud SDK V2.0'ı kullanmanızı öneririz. Daha fazla bilgi için bkz. Alibaba Cloud SDK V1.0 ve V2.0.
Yeni projeler için V2.0'ı kullanmanızı öneririz. V1.0'ın kullanıldığı projelerde ise V1.0'ı V2.0'a yükseltmenizi tavsiye ederiz.
Entegrasyon örneği
Bu örnekte, Java için Elastic Compute Service (ECS) SDK'si entegre edilir ve DescribeInstance işlemi çağrılır.
V2.0
Alibaba Cloud SDK V2.0; parametre işleme, istek oluşturma ve yanıt işleme gibi API işlemlerinde kullanılan temel bilgileri içerir. Geliştiriciler, SDK bağımlılık paketini yükleyerek API işlemlerini çağırabilir ve çekirdek kitaplığa bağımlı kalmak zorunda değildir.
Bağımlılık
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ecs20140526</artifactId>
<version>5.1.0</version>
</dependency>
Örnek kod
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.ecs20140526.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Specify your AccessKey ID.
.setAccessKeyId(accessKeyId)
// Required. Specify your AccessKey secret.
.setAccessKeySecret(accessKeySecret);
// Specify the endpoint.
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
return new com.aliyun.ecs20140526.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
// Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured.
// If the project code is leaked, the AccessKey pair may be disclosed, which compromises the security of resources in your account. The following sample code obtains the AccessKey pair from environment variables. This example is for reference only. We recommend that you use Security Token Service (STS) to initialize SDK clients, which is more secure. For more information about authentication solutions, see the "Manage access credentials" topic for Alibaba Cloud SDK for Java.
com.aliyun.ecs20140526.Client client = Sample.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
com.aliyun.ecs20140526.models.RunInstancesRequest runInstancesRequest = new com.aliyun.ecs20140526.models.RunInstancesRequest();
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// If you copy and run the sample code, write your own code to display the response of the API operation.
client.runInstancesWithOptions(runInstancesRequest, runtime);
} catch (TeaException error) {
// Display the error based on your business requirements.
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// Display the error based on your business requirements.
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
V1.0
Bağımlılıklar
HTTP istekleri, kimlik doğrulama bilgileri, imzalama algoritmaları ve özel durum yönetimi gibi işlevleri içeren SDK çekirdek kitaplığını yüklemeniz gerekir.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ecs</artifactId>
<version>5.11.5</version>
</dependency>
Örnek kod
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import java.util.*;
import com.aliyuncs.ecs.model.v20140526.*;
public class DescribeInstances {
public static void main(String[] args) {
// Create and initialize a DefaultAcsClient instance.
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
IAcsClient client = new DefaultAcsClient(profile);
// Create an API request and configure the required parameters.
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.setRegionId("cn-hangzhou");
request.setInstanceNetworkType("vpc");
request.setInstanceChargeType("PostPaid");
request.setInternetChargeType("PayByTraffic");
request.setPageSize(10);
try {
// Initiate the request and handle the response or exception.
DescribeInstancesResponse response = client.getAcsResponse(request);
for (DescribeInstancesResponse.Instance instance:response.getInstances())
{
System.out.println(instance.getImageId());
System.out.println(instance.getInstanceId());
System.out.println(instance.getPublicIpAddress());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}