How to use ECS SDK after preparing AccessKey

坏嘻嘻
Release: 2018-09-30 13:50:05
forward
2069 people have browsed it

This article introduces how to use the ECS SDK after preparing the AccessKey, and focuses on the specific steps. The content of this article is compact, and I hope you can gain something from it.

ECS SDK Usage Example

The file name of the new version of the SDK usually starts with aliyun-XXXX-sdk, followed by the product name such as ECS, forming a package name such as aliyun-java-sdk-ecs . There is a core package aliyun-java-sdk-core, which encapsulates some classes used by the SDKs of all products, such as IClientProfile class, IAcsClient class, exception class, etc. Product-related classes are packaged into Jar packages with different names on a product basis.

Prerequisites

You need to prepare your AccessKey for output to Create Profile.

Java SDK Usage Example

Take the ECS Java SDK method DescribeImages to query available image resources as an example to introduce the complete process of using the SDK. Among them, the two classes IClientProfile and IAcsClient are included in the aliyun-java-sdk-core package, and the other classes are included in the aliyun-java-sdk-ecs package.

Create Profile. Generate an object profile of IClientProfile, which stores AccessKeyID, AccessKeySecret and default region information, such as cn-hangzhou in the example. For more information about regions, see Regions and Availability Zones.

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

Create Client. The IAcsClient object client is regenerated from the IClientProfile class, and subsequent response needs to be obtained from IClientProfile.

IAcsClient client = new DefaultAcsClient(profile);
Copy after login

Create Request. Create a Request corresponding to the method. The naming rule of the class is generally the API method name plus "Request". For example, the API method to obtain the image list is named DescribeImages, then the corresponding request class name is DescribeImagesRequest. Use the constructor directly to generate a default The class describe.

DescribeImagesRequest describe = new DescribeImagesRequest();
Copy after login

Set the parameters of Request. After the request class is generated, you need to set the necessary information through the setXxx method of the Request class, that is, the information that must be provided in the API parameters. The parameter that must be provided by the API method of DescribeImages is RegionId. This value can be omitted because it is already provided in IClientProfile. For regional information, you can also set other optional parameters through the setXxx method. For example, if you set the image to be queried as a custom image, set the value of ImageOwnerAlias ​​to self, which means querying your custom image.

describe.setImageOwnerAlias("self");
Copy after login

After the parameters are set, the response to the corresponding Request is obtained through the IAcsClient object.

DescribeImagesResponse response = client.getAcsResponse(describe);
Copy after login

Get the returned parameter value in Response. Then you can call the corresponding getXxx method in response to get the returned parameter value, such as getting the name of a certain image. Depending on the API method, the returned information may contain multiple layers of information. For example, through the method of obtaining a mirror list, the mirror in the returned information is represented by a collection, and the collection stores the information of each mirror. For Java As far as the SDK is concerned, the image information is stored in a list. You need to first obtain the collection of Image objects through getImages(), then obtain the information of one of the images through traversal and other methods, and then call the getXxx method to obtain specific information.

for(Image image:response.getImages())
         {
             System.out.println(image.getImageId());
             System.out.println(image.getImageName());
         }
Copy after login

At this point, a complete call is completed.

PHP SDK Notes

The similarities between using PHP SDK and Java SDK can be summarized as follows:

Create Profile.

Create Client.

Create Request.

Set the parameters of Request.

Use the corresponding method of Client to pass in the Request and get the Response.

Get the returned parameter value in Response.

Python SDK Notes

Using the Python SDK omits the step of creating a Profile, create the Client directly, and then perform the following steps.

The above is the detailed content of How to use ECS SDK after preparing AccessKey. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:aliyun.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template