All Products
Search
Document Center

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

Last Updated:Jan 04, 2024

This topic describes the differences between Alibaba Cloud SDK V1.0 and Alibaba Cloud V2.0 in generation method, supported languages, extended support for more languages, and SDK samples for specific scenarios. If you have used Alibaba Cloud SDK V1.0 to integrate Alibaba Cloud services before, you can read this topic to learn about the differences between Alibaba Cloud SDK V1.0 and Alibaba Cloud SDK V2.0. If you are a new user of Alibaba Cloud SDK, we recommend that you directly use Alibaba Cloud SDK V2.0.

V1.0 SDK

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 operation-specific parameters.

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

  3. HTTP Client layer: This layer initiates requests.

To use Alibaba Cloud SDK to integrate an Alibaba Cloud service, you must import the core SDK and service SDK. 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.

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.

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 java.util.*;
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("cn-hangzhou", 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
        **/

        IAcsClient client = new DefaultAcsClient(profile);


        DescribeInstanceStatusRequest request = new DescribeInstanceStatusRequest();
        request.setRegionId("cn-hangzhou");

        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());
        }

    }
}

V2.0 SDK

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

Alibaba Cloud SDK V2.0 is generated based on Darabonba, a domain-specific language (DSL) developed by Alibaba Cloud. 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 abstract syntax trees (ASTs) generated from DSL, it becomes easier to bridge the gap between various API styles and effortlessly generate SDKs from OpenAPI 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 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 consistent user experience.

  • Reduced development costs: 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 then 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

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 an endpoint. For more information, see endpoints at https://api.aliyun.com/product/Ecs.
        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 in the code runtime environment. 
        // If the project code is leaked, the AccessKey pair may be leaked and resources in your account become insecure. The following sample code shows how to use environment variables to obtain an AccessKey pair and use the AccessKey pair to call API operations. The sample code is for reference only. We recommend that you use STS, which provides higher security. For more information about authentication methods, see the "Configure credentials" topic in the documentation of Alibaba Cloud SDK V2.0 for Java. 
        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("cn-hangzhou");
        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);
        }        
    }
}

V1.0 vs V2.0

Comparison item

V1.0 SDK

V2.0 SDK

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 based on Darabonba. Darabonba serves as an intermediary to generate SDKs for different programming languages and automatically verify syntaxes. This provides 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 ASTs in Darabonba.

SDK samples

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

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.

DSL makes it easy to generate SDK samples for specific scenarios in different programming languages. This helps you efficiently learn how to use the SDKs. For more information, visit Sample Code Center.