All Products
Search
Document Center

:Alibaba Cloud SDKs

最終更新日:Feb 01, 2024

This topic describes how to use SDKs of Alibaba Cloud products to call API operations.

Alibaba Cloud SDKs

Alibaba Cloud provides SDKs in multiple programming languages, including Java, C#, Go, Python, TypeScript + Node.js, PHP, and C++. APIs can be directly called through the SDKs integrated into your applications. SDK encapsulates information including signature logic, timeout mechanism, and retry mechanism. It returns structured response objects based on specifications, which provides convenience for developers. For more information, see Alibaba Cloud API overview.

Integration method

  1. Go to OpenAPI Explorer, click the image.png icon in the top navigation bar, and search for the cloud product that you use.

  2. Click SDK in the top navigation bar, and select the programming language that you use.

  3. Check the SDK installation method and sample code to integrate the SDK.

Comparison of V2.0 and V1.0

The Alibaba Cloud SDK V2.0 supports more languages than the Alibaba Cloud SDK V1.0 and more complex scenarios of API operations. V2.0 supports both asynchronous and synchronous calls and has solved some legacy issues in the original version, providing more flexible and powerful features. We recommend that you use V2.0. For more information, see Differences between Alibaba Cloud SDK V1.0 and Alibaba Cloud SDK V2.0

Note

For new projects, we recommend that you use V2.0. For projects where V1.0 has been used, an upgrade is recommended.

Integration example

In this example, ECS SDK for Java is used to call the DescribeInstance operation in Elastic Compute Service (ECS).

V2

V2.0 includes the main information that is used for API operations, such as parameter processing, request assembly, and response processing. Developers can call API operations by installing the SDK dependency package and do not need to rely on the core library.

Dependency

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>alibabacloud-ecs20140526</artifactId>
  <version>2.0.5</version>
</dependency>

Sample code

// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.sample;

import com.aliyun.tea.*;

public class Sample {

    /**
     * Use your AccessKey ID and AccessKey secret to initialize the client.
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.ecs20140526.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Required. Specify your AccessKey ID.
                .setAccessKeyId(accessKeyId)
                // Required. Specify your AccessKey secret.
                .setAccessKeySecret(accessKeySecret);
        // Specify the endpoint.
        config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ecs20140526.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. 
        // If the project code is leaked, the AccessKey pair may be leaked and security issues may occur on all resources of your account. The following sample code obtains the AccessKey pair from the environment variables to call the operation. This method is for reference only. We recommend that you use STS tokens, which provides higher security. For more information on authentication, see https://help.aliyun.com/document_detail/378657.html.
        com.aliyun.ecs20140526.Client client = Sample.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        com.aliyun.ecs20140526.models.RunInstancesRequest runInstancesRequest = new com.aliyun.ecs20140526.models.RunInstancesRequest();
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // After you copy and run the sample code, obtain the return value of the operation.
            client.runInstancesWithOptions(runInstancesRequest, runtime);
        } catch (TeaException error) {
            // Print the error if necessary.
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // Print the error if necessary.
            com.aliyun.teautil.Common.assertAsString(error.message);
        }        
    }
}

V1

Install a dependency

The SDK core library must be installed, which includes HTTP requests, authentication information, signing algorithms, and exception handling.

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.6.1</version>
</dependency>
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-ecs</artifactId>
  <version>4.24.64</version>
</dependency>

Sample 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 java.util.*;
import com.aliyuncs.ecs.model.v20140526.*;

public class DescribeInstances {

    public static void main(String[] args) {
        // Create and initialize a DefaultAcsClient instance. 
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<yourAccessKeyId>", "<yourAccessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        // Create an API request and configure the required parameters. 
        DescribeInstancesRequest request = new DescribeInstancesRequest();
        request.setRegionId("cn-hangzhou");
        request.setInstanceNetworkType("vpc");
        request.setInstanceChargeType("PostPaid");
        request.setInternetChargeType("PayByTraffic");
        request.setPageSize(10);

        try {
        // Initiate the request and handle the response and exceptions. 
            DescribeInstancesResponse response = client.getAcsResponse(request);
        for (DescribeInstancesResponse.Instance instance:response.getInstances()) 
            {
                 System.out.println(instance.getImageId());
                 System.out.println(instance.getInstanceId());
                 System.out.println(instance.getPublicIpAddress());
             }
        } 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());
        }

    }
}