Alibaba Cloud SDKs
Alibaba Cloud provides SDKs in multiple programming languages, including Java, C#, Go, Python, TypeScript + Node.js, PHP, and C++. APIs can be directly called through the SDKs integrated into your applications. SDK encapsulates information including signature logic, timeout mechanism, and retry mechanism. It returns structured response objects based on specifications, which provides convenience for developers. For more information, see Alibaba Cloud API overview.
Integration method
Go to OpenAPI Explorer, click the
icon in the top navigation bar, and search for the cloud product that you use.
Click SDK in the top navigation bar, and select the programming language that you use.
Check the SDK installation method and sample code to integrate the SDK.
Comparison of V2.0 and V1.0
The Alibaba Cloud SDK V2.0 supports more languages than the Alibaba Cloud SDK V1.0 and more complex scenarios of API operations. V2.0 supports both asynchronous and synchronous calls and has solved some legacy issues in the original version, providing more flexible and powerful features. We recommend that you use V2.0. For more information, see Differences between Alibaba Cloud SDK V1.0 and Alibaba Cloud SDK V2.0
Note
For new projects, we recommend that you use V2.0. For projects where V1.0 has been used, an upgrade is recommended.
Integration example
In this example, ECS SDK for Java is used to call the DescribeInstance
operation in Elastic Compute Service (ECS).
V2
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>alibabacloud-ecs20140526</artifactId>
<version>2.0.5</version>
</dependency>
Sample code
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
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()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
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_);
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 {
client.runInstancesWithOptions(runInstancesRequest, runtime);
} catch (TeaException error) {
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
V1
Install a dependency
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>4.24.64</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) {
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<yourAccessKeyId>", "<yourAccessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.setRegionId("cn-hangzhou");
request.setInstanceNetworkType("vpc");
request.setInstanceChargeType("PostPaid");
request.setInternetChargeType("PayByTraffic");
request.setPageSize(10);
try {
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());
}
}
}