This topic describes how to install and use Alibaba Cloud SDK. Alibaba Cloud SDK for Java takes the complexity out of coding and allows you to quickly access Alibaba Cloud services such as Elastic Compute Service (ECS), ApsaraDB for Relational Database Service (RDS), and CloudMonitor.
Debug and generate SDK examples online
You can use OpenAPI Explorer to call APIs of cloud services, dynamically generate SDK sample codes, and quickly retrieve interfaces.
Prerequisites
- To use Alibaba Cloud SDK for Java, you must have an Alibaba Cloud account and an AccessKey. You can create and view your AccessKey pair on the AccessKey Management page in the Alibaba Cloud Console, or you can contact your system administrator.
- Before using Alibaba Cloud SDK for Java to call the APIs of a product, make sure that you have activated this product in the Alibaba Cloud Console.
- Java is installed on your system. The Alibaba Cloud SDK for Java supports JDK1.6 or later.
Install Alibaba Cloud SDK for Java
You can install Alibaba Cloud SDK for Java by adding Maven dependencies or by downloading the SDK tools for Alibaba Cloud SDK for Java. For more information, see Install Alibaba Cloud SDK for Java.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.6</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ecs</artifactId>
<version>4.17.6</version>
</dependency>
Use Alibaba Cloud SDK for Java
The following sample code shows the three main steps to use Alibaba Cloud SDK for Java:
- Create a DefaultAcsClient instance and initialize it.
- Create an API request and set parameters.
- Initiate a request and process the response or exceptions.
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.ecs.model.v20140526.*;
public class Demo {
public static void main(String[] args) {
//Create a DefaultAcsClient instance and initialize it.
DefaultProfile profile = DefaultProfile.getProfile(
"<your-region-id>", // Region ID
"<your-access-key-id>", // The AccessKey ID of the RAM account
"<your-access-key-secret>"); // The AccessKey Secret of the RAM account
IAcsClient client = new DefaultAcsClient(profile);
// Create an API request and set its parameters
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.setPageSize(10);
// Initiate the request and handle the response or exception
DescribeInstancesResponse response;
try {
response = client.getAcsResponse(request);
for (DescribeInstancesResponse.Instance instance:response.getInstances()) {
System.out.println(instance.getPublicIpAddress());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}