Get started with Alibaba Cloud SDK V1.0 for Go

更新时间:
复制 MD 格式

Install the Alibaba Cloud SDK V1.0 for Go, configure credentials, and make your first API call.

SDK version notes

Alibaba Cloud SDK V1.0 for Go requires Go 1.10 or later. V1.0 is a stable, production-ready release used by many existing Alibaba Cloud customers.

If you are starting a new project, use Alibaba Cloud SDK V2.0 for Go (Darabonba SDK) instead. V2.0 offers significant improvements in usability and maintainability. If you are an existing V1.0 user, migrate to V2.0 as early as possible. For migration guidance, see Get started with Alibaba Cloud Darabonba SDK for Go.

Prerequisites

Before you begin, ensure that you have:

  • Go 1.10 or later installed. Run go version to check.

  • An Alibaba Cloud account with an AccessKey pair.

Step 1: Install the SDK

Initialize a Go module for your project:

go mod init example

Then install the SDK:

go get github.com/aliyun/alibaba-cloud-sdk-go/sdk

Step 2: Get your AccessKey

The SDK authenticates API requests using an AccessKey pair:

  • AccessKey ID: identifies the caller (similar to a username).

  • AccessKey Secret: signs requests to prove the caller's identity (similar to a password). Keep this value private.

To create or retrieve your AccessKey pair:

  1. Log on to the Alibaba Cloud Management Console.

  2. Click your avatar in the upper-right corner and select AccessKey Management.

  3. On the AccessKey Management page, click Create AccessKey.

  4. Copy the AccessKey ID and AccessKey Secret. Your credentials look similar to the following:

    • AccessKey ID: LTAI5tExampleAccessKeyId

    • AccessKey Secret: ExampleAccessKeySecretAbcdef

  5. Store the credentials in a secure location.

    Warning

    Never hard-code credentials in source code or commit them to version control. Use environment variables or a secrets manager to inject credentials at runtime.

Step 3: Make your first API call

The following example lists the Elastic Compute Service (ECS) instances in the cn-hangzhou region. All SDK calls follow the same pattern: load credentials, create a client for the target service, build a request, and handle the response.

Set your credentials as environment variables before running the example:

export ALIBABA_CLOUD_ACCESS_KEY_ID=<your-access-key-id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<your-access-key-secret>

Placeholder

Description

Example

<your-access-key-id>

Your Alibaba Cloud AccessKey ID

LTAI5tExampleAccessKeyId

<your-access-key-secret>

Your Alibaba Cloud AccessKey Secret

ExampleAccessKeySecretAbcdef

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)

func main() {
	// Load credentials from environment variables
	accessKeyID := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
	accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")

	// Create an ECS client for the cn-hangzhou region
	client, err := ecs.NewClientWithAccessKey("cn-hangzhou", accessKeyID, accessKeySecret)
	if err != nil {
		log.Fatalf("Failed to create ECS client: %v", err)
	}

	// Build and send the DescribeInstances request
	request := ecs.CreateDescribeInstancesRequest()
	request.Scheme = "https"

	response, err := client.DescribeInstances(request)
	if err != nil {
		log.Fatalf("DescribeInstances failed: %v", err)
	}

	// Print instance IDs and names
	fmt.Printf("Total instances: %d\n", response.TotalCount)
	for _, instance := range response.Instances.Instance {
		fmt.Printf("  ID: %s  Name: %s\n", instance.InstanceId, instance.InstanceName)
	}
}

What's next

  • Explore service-specific clients: the SDK includes clients for ECS, OSS, RDS, and other Alibaba Cloud services under github.com/aliyun/alibaba-cloud-sdk-go/services/.

  • Migrate to V2.0: the Darabonba SDK (V2.0) offers significant improvements in usability and maintainability. For migration guidance, see the V2.0 getting-started guide linked in the SDK version notes section above.