This topic describes how to use a developer tool such as Alibaba Cloud CLI, OpenAPI Explorer, or Alibaba Cloud SDK to call Elastic Compute Service (ECS) API operations. In the examples, CreateSnapshot is used.
Background information
Before you call an API operation, you must understand related instructions in the API documentation and obtain the values of the required request parameters. If an error code is returned after you send a request, you can obtain the description of the error code in the API documentation.
Methods used to make API requests
- Example of calling an ECS API operation by using Alibaba Cloud CLI: Alibaba Cloud CLI is applicable to scenarios where command line tools are frequently used. Make sure that Alibaba Cloud CLI is installed on the ECS instance. For more information about how to install Alibaba Cloud CLI in different operating systems, see the following topics:
- Example of calling an ECS API operation by using OpenAPI Explorer: OpenAPI Explorer is applicable to users who frequently use interactive operation interfaces or developers who are unfamiliar with Alibaba Cloud services. You can debug API operations and obtain sample SDK requests in OpenAPI Explorer. For more information about OpenAPI Explorer, see What is OpenAPI Explorer?
- Example of calling an ECS API operation by using ECS SDK for Java: ECS SDK for Java is applicable to scenarios such as SDK encoding or DevOps. To use ECS SDK for Java, make sure that the SDK is installed. For more information, see Install ECS SDK for Java.
Example of calling an ECS API operation by using Alibaba Cloud CLI
Example of calling an ECS API operation by using OpenAPI Explorer
Example of calling an ECS API operation by using ECS SDK for Java
You must specify the values of the following parameters in the sample code:
- <AccessKey>: Enter your AccessKey ID. For information about how to obtain your AccessKey pair, see Create an AccessKey pair.
- <AccessSecret>: Enter your AccessKey secret.
- <RegionId>: Enter the region ID of the instance. For information about the valid values, see Regions and zones or DescribeRegions.
- <DiskId>: Enter the ID of the disk. For information about the valid values, see DescribeDisks.
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.CreateSnapshotRequest;
import com.aliyuncs.ecs.model.v20140526.CreateSnapshotResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
/* pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.0.9</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ecs</artifactId>
<version>4.10.1</version>
</dependency>
*/
public class CreateSnapshotExample {
private String accessKeyId = "<AccessKey>";
private String accessSecret = "<AccessSecret>";
/**
* The region ID of the disk
*/
private String regionId = "<RegionId>";
/**
* The ID of the disk for which you want to create a snapshot
*/
private String diskId = "<DiskId>";
public void createSnapshot() {
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
IAcsClient client = new DefaultAcsClient(profile);
CreateSnapshotRequest request = new CreateSnapshotRequest();
request.setRegionId(regionId);
request.setDiskId(diskId);
try {
CreateSnapshotResponse response = client.getAcsResponse(request);
logInfo(response.getSnapshotId());
} catch (ServerException e) {
logInfo(String.format("Fail. Something with your connection with Aliyun go incorrect. ErrorCode: %s",
e.getErrCode()));
} catch (ClientException e) {
logInfo(String.format("Fail. Business error. ErrorCode: %s, RequestId: %s",
e.getErrCode(), e.getRequestId()));
}
}
private static void logInfo(String message) {
System.out.println(message);
}
public static void main(String[] args) {
new CreateSnapshotExample().createSnapshot();
}
}