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.
You must have your Accesskey ready.
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.
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.
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);
Create the request class: create a request class that corresponds to the method, and name the class by adding
Requestto 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();
Specify request parameters: specify required API parameters by using the setter
setXxx. For example, the DescribeImages API method requires the
RegionIdparameter, which is optional, because IClientProfile already contains region information. Use other setters to set other parameters. For example, to query your custom images, set
Obtain the response to the request using the IAcsClient object.
DescribeImagesResponse response = client.getAcsResponse(describe);
Obtain the response parameters from the response: call the getter
getXxxin 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
getXxxto obtain more information.
The call process is now concluded.
To use PHP SDKs is the same way as to use Java SDKs. Follow these steps:
- Create a profile object.
- Create a client object.
- Create a request object.
- Set parameters for the request.
- Call the corresponding API method on the client object by passing in the request object and obtain the response.
- Obtain response values in the response.
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.