This topic describes how to use SDKs of Alibaba Cloud services to call API operations.
Alibaba Cloud SDKs
Alibaba Cloud provides SDKs in multiple programming languages, including Java, C#, Go, Python, TypeScript + Node.js, PHP, and C++. API operations can be directly called through the SDKs integrated into your applications. SDKs encapsulate information including signature logic, timeout mechanism, and retry mechanism and return structured response objects based on specifications, which provide convenience for developers.
For more information about Alibaba Cloud SDKs, see Alibaba Cloud SDKs.
Integration method
Visit OpenAPI Explorer. Move the pointer over
in the top navigation bar and find the required service. In the SDK Examples section, click the programming language that you use.
View the SDK installation method and sample code to integrate the SDK.
Comparison of V2.0 and V1.0
Alibaba Cloud SDK V2.0 supports more languages and more complex scenarios of API operations than Alibaba Cloud SDK V1.0. V2.0 supports both asynchronous and synchronous calls and has solved some legacy issues in V1.0. V2.0 provides more flexible and powerful features. We recommend that you use Alibaba Cloud SDK V2.0. For more information, see Alibaba Cloud SDK V1.0 and V2.0.
For new projects, we recommend that you use V2.0. For projects in which V1.0 is used, we recommend that you upgrade V1.0 to V2.0.
Integration example
In this example, Elastic Compute Service (ECS) SDK for Java is integrated and used to call the DescribeInstance operation.
V2.0
Alibaba Cloud SDK V2.0 includes the main information that is used for API operations, such as parameter processing, request assembly, and response processing. Developers can call API operations by installing the SDK dependency package and do not need to rely on the core library.
Dependency
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ecs20140526</artifactId>
<version>5.1.0</version>
</dependency>Sample code
// 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
Dependencies
The SDK core library must be installed, which includes HTTP requests, authentication information, signing algorithms, and exception handling.
<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>Sample code
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());
}
}
}