edit-icon download-icon

Samples of using ECS SDK

Last Updated: Jul 14, 2018

The file name of the new version SDK usually starts with aliyun-XXXX-sdk, followed by the product name, for example the package name aliyun-java-sdk-ecs. The aliyun-java-sdk-core core package encapsulates certain classes used in all product SDKs, such as the IClientProfile, IAcsClient, and exception classes. The classes are packed in different JAR packages by product.

Prerequisites

You must have your Accesskey ready.

Sample of using the Java SDK

The API method DescribeImages is used to query available image resources in this sample. We will use it as an example to demonstrate the process of using Java SDKs. The aliyun-java-sdk-core package contains the IClientProfile and IAcsClient classes, and the aliyun-java-sdk-ecs package contains other classes.

  1. Create a profile object: create an instance of the IClientProfile class named profile, which contains AccessKeyID, AccessKeySecret, and the default region information, such as cn-hangzhou. For more information about the Alibaba Cloud regions, see Regions and zones.

    IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ak, aks); #ak is your AccessKey, and aks is your AccessKeySecret.

  2. Create a client object: create an IAcsClient object named client from the previously created IClientProfile profile, and subsequent responses are obtained from IClientProfile. This object contains the response data that you will retrieve later.

    IAcsClient client = new DefaultAcsClient(profile);

  3. Create the request class: create a request class that corresponds to the method, and name the class by adding Request to the end of an API method name. For example, if the name of the API method that queries the image lists is DescribeImages, the corresponding Request class name is DescribeImagesRequest. It uses a constructor to generate a default describe class.

    DescribeImagesRequest describe = new DescribeImagesRequest();

  4. Specify request parameters: specify required API parameters by using the setter setXxx. For example, the DescribeImages API method requires the RegionId parameter, which is optional, because IClientProfile already contains region information. Use other setters to set other parameters. For example, to query your custom images, set ImageOwnerAlias to self.

    describe.setImageOwnerAlias("self");

  5. Obtain the response to the request using the IAcsClient object.

    DescribeImagesResponse response = client.getAcsResponse(describe);

  6. Obtain the response parameters from the response: call the getter getXxx in the response to obtain the response values, such as the ImageName. For different API methods, a return value may contain structured information. For example, in the method DescribeImages, the response values include a collection of images in the Java SDK. Call the getImages() method to query the image object list, iterate through the list to query an image, and then call the getXxx to obtain more information.

    1. for(Image image:response.getImages())
    2. {
    3. System.out.println(image.getImageId());
    4. System.out.println(image.getImageName());
    5. }

The call process is now concluded.

Notes for the PHP SDK

To use PHP SDKs is the same way as to use Java SDKs. Follow these steps:

  1. Create a profile object.
  2. Create a client object.
  3. Create a request object.
  4. Set parameters for the request.
  5. Call the corresponding API method on the client object by passing in the request object and obtain the response.
  6. Obtain response values in the response.

Notes for the Python SDK

When you use Python SDKs, you do not have to create a profile. Instead, create a client directly, and then continue with the remaining steps.

References

  • For more information about the available API methods of ECS, see API overview.
  • For more information about how to create an AccessKey, see Create an AccessKey.
Thank you! We've received your feedback.