Alibaba Cloud SDK for Go

repository·master·Indexed 22 days ago

https://github.com/aliyun/alibaba-cloud-sdk-go

The Alibaba Cloud SDK for Go allows developers to interact with Alibaba Cloud services via OpenAPI, supporting both ROA (RESTful) and RPC request styles. It requires Go 1.13.x or higher. Note: The V1.0 SDK entered End-of-Support on March 1, 2025; new users and existing users are advised to migrate to the V2.0 Go SDK.

Tokens
109.1K
Snippets
656
Records
697
Agent score
75%

What's inside alibaba-cloud-sdk-go

  1. How the Default Credential Provider Chain works

    master

    If no client is explicitly created before a request, the SDK automatically attempts to create a default client using the following order of precedence:

    1. Environment Credentials: Checks for ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
    2. Credentials File: Looks for a configuration file at ~/.alibabacloud/credentials (or %UserProfile%\.alibabacloud\credentials on Windows). The file path can be overridden by setting the ALIBABA_CLOUD_CREDENTIALS_FILE environment variable.
    3. Instance RAM Role: If ALIBABA_CLOUD_ECS_METADATA is set, the SDK uses that value as a role name to fetch temporary credentials from the ECS metadata service (http://100.100.100.200/latest/meta-data/ram/security-credentials/).
  2. Configure SSL certificate verification

    master

    You can control whether the SDK verifies SSL certificates for HTTPS requests. This can be configured at both the individual request level and the global client level.

    • Enable verification (Default): Set HTTPSInsecure to false. The SDK will use the CA packages provided by your operating system to verify certificates.
    • Disable verification (Insecure): Set HTTPSInsecure to true. This disables certificate validation. Warning: This is insecure and should only be used for testing; in production, you should use valid certificates.
  3. Important: Migration to V2.0 Go SDK

    master

    Deprecation Notice

    The Alibaba Cloud V1.0 Go SDK entered End-of-Support on March 1, 2025 and is no longer recommended for use.

    Actions required:

    • New users: Use the V2.0 Go SDK directly.
    • Existing users: It is advised to migrate from V1.0 to the V2.0 Go SDK.

    The V2.0 Go SDK is available at https://github.com/alibabacloud-go. For usage details, refer to the Alibaba Cloud Help Center Documentation.

  4. Important: Migration notice for Alibaba Cloud Go SDK V1.0

    master

    Alibaba Cloud V1.0 Go SDK is reaching end-of-life on March 1, 2025, and its use is no longer recommended.

    For usage details, refer to the Alibaba Cloud Help Center and the Alibaba Cloud OpenAPI Developer Portal.

  5. Understand the endpoint search priority logic

    master

    The SDK follows a specific hierarchy to resolve which endpoint to use for an API call:

    1. User Custom Definition: The highest priority. This includes global mappings, request-level Domain settings, or client-level Domain settings.
    2. Endpoint Splicing Rules: If VPC is enabled or the product SDK has an endpoint data file, the SDK uses the client.Network setting to splice the endpoint.
    3. SDK Core Data File: The SDK searches for endpoint information in the endpoints_config.go file within the Go SDK Core. This is an internal operation and requires no manual configuration.
    4. Remote Location Service: If the product SDK includes a ServiceCode, the SDK can request the endpoint from a remote Location Service API.
  6. Configure client-wide timeouts

    master

    You can set default timeouts for all requests sent via a specific client. Note that client-level timeouts only apply if the individual request has not already defined its own timeout.

    Use SetReadTimeout(duration) and SetConnectTimeout(duration) on the client instance to configure them, and GetReadTimeout() or GetConnectTimeout() to retrieve the current values.

    // 设置客户端超时(对所有通过该客户端发送的请求生效)
    client.SetReadTimeout(10 * time.Second)             // 设置客户端读超时为10秒
    readTimeout := client.GetReadTimeout()              // 获取客户端读超时
    client.SetConnectTimeout(5 * time.Second)           // 设置客户端连接超时为5秒
    connectTimeout := client.GetConnectTimeout()        // 获取客户端连接超时
  7. Create a client with AccessKey

    master

    To interact with Alibaba Cloud services, you must first sign up for an account and obtain your credentials. Use sdk.NewClientWithAccessKey to initialize the client with your REGION_ID, ACCESS_KEY_ID, and ACCESS_KEY_SECRET.

    package main
    
    import "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    
    func main() {
      client, err := sdk.NewClientWithAccessKey("REGION_ID", "ACCESS_KEY_ID", "ACCESS_KEY_SECRET")
      if err != nil {
        // Handle error
        panic(err)
      }
    }
  8. Enable asynchronous calls in Alibaba Cloud SDK for Go

    master

    The Alibaba Cloud SDK for Go supports asynchronous calls through two methods. Note that once asynchronous calls are enabled, you must call Shutdown() before you can enable them again.

    Method 1: During Client Initialization

    Configure the sdk.Config object before creating the client. You can specify the number of goroutines and the maximum task queue size per goroutine.

    Method 2: Using EnableAsync

    Call EnableAsync(goroutinePoolSize, maxTaskQueueSize) on an existing client instance. This method can only be called once unless Shutdown() has been called first.

    // Method 1: During Initialization
    import (
        "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
        "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
        "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
        "time"
    )
    
    c := sdk.NewConfig()
    c.EnableAsync = true        // Enable asynchronous tasks
    c.GoRoutinePoolSize = 10    // Number of goroutines to start
    c.MaxTaskQueueSize = 20    // Max tasks per goroutine
    c.Timeout = 10 * time.Second
    credential := credentials.NewAccessKeyCredential("acesskeyid", "accesskeysecret")
    client, err := ecs.NewClientWithOptions("regionid", c, credential)
    
    // Method 2: Using EnableAsync on existing client
    // First param: goroutine pool size
    // Second param: max tasks per goroutine
    client.EnableAsync(10, 20)