This topic describes how to use the ECS Java SDK by taking DescribeImages as an example. DescribeImages is the operation used by the ECS Java SDK to query available image resources.
Prerequisites
You have created an AccessKey pair. For information about how to create an AccessKey
pair, see Create an AccessKey.
Note To protect the AccessKey pair of your Alibaba Cloud account, we recommend that you
create a RAM user, grant the RAM user the permissions to access ECS instances, and
then use the AccessKey pair of the RAM user to call the Java SDK. For more information,
see Implement access control by using RAM.
Background information
- In this example, the IClientProfile and IAcsClient classes are included in aliyun-java-sdk-core, and the other classes are included in aliyun-java-sdk-ecs.
- The purpose of this example is to query ECS public images. For information about public images, see image related documentation. For more information, see Overview.
- The following table compares the methods used by the previous SDK and by the new SDK,
their classes, and objects. If you are using the previous SDK, we recommend that you
switch to the new version to obtain the new features.
Item New SDK Earlier SDK Submit a request getAcsResponse() execute() Class that stores the AccessKey pair IClientProfile AliyunClient Objects for storing identity credentials DefaultProfile.getProfile(RegionId, AccessKey, AccessKeySecret) new DefaultAliyunClient(APIUrl, AccessKey, AccessKeySecret) Package name prefix com.aliyuncs com.aliyun.api
Procedure
Result
- The complete response is as follows:
{ "PageNumber": 1, "TotalCount": 43, "PageSize": 1, "RegionId": "cn-hangzhou", "RequestId": "C93F3D9F-CF25-47DF-9C0F-614395E5DCAC", "Images": { "Image": [ { "ImageId": "freebsd_11_02_64_30G_alibase_20190722.vhd", "Description": "", "OSNameEn": "FreeBSD 11.2 64 bit", "ProductCode": "", "ResourceGroupId": "", "OSType": "linux", "Architecture": "x86_64", "OSName": "FreeBSD 11.2 64-bit", "DiskDeviceMappings": { "DiskDeviceMapping": [] }, "ImageOwnerAlias": "system", "Progress": "100%", "IsSupportCloudinit": false, "Usage": "instance", "CreationTime": "2019-07-23T05:41:06Z", "Tags": { "Tag": [] }, "ImageVersion": "", "Status": "Available", "ImageName": "freebsd_11_02_64_30G_alibase_20190722.vhd", "IsSupportIoOptimized": true, "IsSelfShared": "", "IsCopied": false, "IsSubscribed": false, "Platform": "Freebsd", "Size": 30 } ] } }
- Obtain the query results of specific returned parameters, such as ImageId and ImageName:
freebsd_11_02_64_30G_alibase_20190722.vhd freebsd_11_02_64_30G_alibase_20190722.vhd
Sample code
The following example shows complete Java SDK 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 com.alibaba.fastjson.JSON;
import java.util.*;
import com.aliyuncs.ecs.model.v20140526.*;
public class DescribeImages {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAjVUwKznS*****", "BNPO1zoNSi484oizGM9fzzwJJ*****");
IAcsClient client = new DefaultAcsClient(profile);
DescribeImagesRequest request = new DescribeImagesRequest();
request.setRegionId("cn-hangzhou");
request.setImageOwnerAlias("system");
request.setPageNumber(1);
request.setPageSize(1);
try {
DescribeImagesResponse response = client.getAcsResponse(request);
System.out.println(JSON.toJSONString(response));
for(DescribeImagesResponse.Image image:response.getImages())
{
System.out.println(image.getImageId());
System.out.println(image.getImageName());
}
} 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());
}
}
}