All Products
Search
Document Center

Alibaba Cloud SDK:Alibaba Cloud SDK V1.0 and V2.0

Last Updated:Jul 10, 2024

This topic describes the differences between Alibaba Cloud SDK V1.0 and V2.0 in terms of the generation method, supported languages, extended support for more languages, and SDK samples for specific scenarios. If you have previously integrated Alibaba Cloud services by using Alibaba Cloud SDK V1.0 and find that the documentation differs from what you are used to, you can read this topic to understand the differences between Alibaba Cloud SDK V1.0 and V2.0. If you are new to Alibaba Cloud SDK, we recommend that you start with Alibaba Cloud SDK V2.0.

Comparison between Alibaba Cloud SDK V1.0 and V2.0

Item

Alibaba Cloud SDK V1.0 (not recommended)

Alibaba Cloud SDK V2.0 (recommended)

Generation method

SDKs for different programming languages are generated based on different templates. The features encapsulated in the SDKs vary with the programming language. Therefore, you have different user experience with SDKs for different programming languages.

SDKs are generated by using Darabonba, a domain-specific language (DSL). Darabonba serves as an intermediary to generate SDKs for different programming languages and automatically verify syntax. This provides a consistent user experience when you use SDKs for different programming languages.

Supported languages

For most Alibaba Cloud services, only SDKs for up to three mainstream programming languages are provided.

SDKs for six mainstream programming languages are provided.

Extended support for more languages

It is difficult to generate SDKs for more programming languages because SDKs are generated based on templates.

SDKs for more programming languages can be easily generated by developing abstract syntax trees (ASTs) in Darabonba.

SDK samples

The sample code for different programming languages differs from each other. The correctness of the sample code cannot be ensured.

The sample code for different programming languages is written in an intermediate language based on the same logic. All the sample code is verified to ensure correctness.

SDK samples for specific scenarios

SDK samples for specific scenarios are difficult to write. Only a few services provide SDK samples for specific scenarios in a limited number of programming languages.

You can use Darabonba to generate SDK samples for specific scenarios in different programming languages at a time. This helps you efficiently learn how to use the SDKs. For more information, visit Sample Code Center.

Alibaba Cloud SDK V1.0 (not recommended)

Note

We recommend that you do not use Alibaba Cloud SDK V1.0 due to issues such as the inconsistent user experience and the inability to isolate authentication information.

The following figure shows the basic architecture of Alibaba Cloud SDK V1.0.

image

As shown in the architecture, Alibaba Cloud SDK V1.0 consists of the following layers:

  1. Service SDK layer: This layer processes API operation-specific parameters.

  2. Core SDK layer: This layer converts API operation-specific parameters to HTTP request parameters.

  3. HTTP Client layer: This layer initiates requests.

To use Alibaba Cloud SDK V1.0 to integrate Alibaba Cloud services, you must import the core SDK and service SDK.

Sample code

When you call API operations of multiple Alibaba Cloud services at the same time, you cannot isolate the common client objects by service. This may cause thread safety issues.

<!-- ECS sdk -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-ecs</artifactId>
    <version>5.11.7</version>
</dependency>
<!-- core sdk -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.6.1</version>
</dependency>
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.google.gson.Gson;
import com.aliyuncs.ecs.model.v20140526.*;
public class DescribeInstanceStatus {

    public static void main(String[] args) {
        // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
        DefaultProfile profile = DefaultProfile.getProfile("<YOUR-REGION-ID>", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        /** use STS Token
        DefaultProfile profile = DefaultProfile.getProfile(
            "<YOUR-REGION-ID>",           // The region ID
            System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),       // The AccessKey ID of the RAM account
            System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),   // The AccessKey Secret of the RAM account
            System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN"));     // STS Token
        **/

        // init acs client
        IAcsClient client = new DefaultAcsClient(profile);

        // Create the request to query instance status
        DescribeInstanceStatusRequest request = new DescribeInstanceStatusRequest();
        request.setRegionId("<YOUR-REGION-ID>");

        try {
            DescribeInstanceStatusResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } 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());
        }
    }
}

Alibaba Cloud SDK V2.0 (recommended)

Alibaba Cloud SDK V1.0 is an earlier version that is widely used by developers. Alibaba Cloud SDK V2.0 is developed to resolve a series of issues that developers encounter when they use Alibaba Cloud SDK V1.0. Alibaba Cloud SDK V2.0 provides many new features to simplify the use of SDKs, reduce integration costs, and improve the robustness of SDKs.

Alibaba Cloud SDK V2.0 is generated by using Darabonba, a DSL developed by Alibaba Cloud. The DSL offers flexibility not only for expressing a wider range of concepts but also for abstracting the differences in API styles across different Alibaba Cloud services. By parsing the ASTs generated from the DSL, it becomes easier to bridge the gap between various API styles and effortlessly generate SDKs from API specifications. Compared with Alibaba Cloud SDK V1.0, Alibaba Cloud SDK V2.0 provides the following benefits:

  • Consistent user experience: Some Alibaba Cloud services provide APIs in the remote procedure call (RPC) style, whereas other Alibaba Cloud services provide APIs in the resource-oriented architecture (ROA) style. The differences in API styles cause an inconsistent user experience when developers use Alibaba Cloud SDK V1.0 to integrate Alibaba Cloud services. In Alibaba Cloud SDK V2.0, the SDKs of all Alibaba Cloud services are used in the same way. This ensures a consistent user experience.

  • Reduced development costs: The DSL allows Alibaba Cloud SDK V2.0 to describe business logic. This resolves the issue that Alibaba Cloud SDK V1.0 is tightly coupled with the core library. This way, the development costs caused by SDK updates are reduced.

  • Isolated identity and authentication information: Each Alibaba Cloud service provides an SDK client object. You can instantiate a client object to call all API operations of an Alibaba Cloud service. This implementation of Alibaba Cloud SDK V2.0 is better than that of Alibaba Cloud SDK V1.0. In Alibaba Cloud SDK V1.0, an SDK client processes multiple threads, and permission management is less efficient. In contrast, Alibaba Cloud SDK V2.0 ensures the security of multiple threads, isolates the identity and authentication information about each Alibaba Cloud service, and does not require multiple Alibaba Cloud services to share one piece of user profile information. You can configure different parameters to call the API operations of different Alibaba Cloud services. The parameters that you can configure include the region, timeout period, HTTP proxy, and retry mechanism.

  • Complex scenarios: Alibaba Cloud SDK V2.0 supports API calls in more complex scenarios. For example, to use the FaceBody feature of Visual Intelligence API, you must upload an image to Object Storage Service (OSS) and use AI to analyze the image based on the generated image URL. In this case, Alibaba Cloud SDK V2.0 allows you to combine the authentication, image upload, and image analysis operations. This simplifies API calls.

  • SDK samples for specific scenarios: Alibaba Cloud SDK V2.0 provides sample code for all API requests. Alibaba Cloud SDK V2.0 also provides sample code for scenarios in which multiple API requests are sent at a time. The provided information helps you use SDKs with ease and better understand business scenarios in which you can use Alibaba Cloud services.

If you are a new user, we recommend that you directly use Alibaba Cloud SDK V2.0. If you are an existing user of Alibaba Cloud SDK V1.0, we recommend that you update your SDKs to the latest version at the earliest opportunity.

Sample code

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>ecs20140526</artifactId>
  <version>5.1.8</version>
</dependency>
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);
        config.regionId = "<YOUR-REGION-ID>";
        return new com.aliyun.ecs20140526.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        // Make sure that the following environment variables are configured in the runtime of code: ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 
        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.DescribeInstanceStatusRequest describeInstanceStatusRequest = new com.aliyun.ecs20140526.models.DescribeInstanceStatusRequest()
                .setRegionId("<YOUR-REGION-ID>");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // If you copy and run the sample code, write your own code to display the response of the API operation.
            client.describeInstanceStatusWithOptions(describeInstanceStatusRequest, runtime);
        } catch (TeaException error) {
            // Display the error based on your business requirements.
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // Display the error based on your business requirements.
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

View the dependency information about an SDK

  1. Go to OpenAPI Portal and select a service. In this example, Virtual Private Cloud (VPC) is used.

image

  1. Select an SDK version.

image

  1. Select a language. In the Installation Method section, you can view the SDK dependencies for the language selected for the service. In the Overview section, you can view the latest SDK version.

image