Integrate with Alibaba Cloud SDK V1.0 for Go

更新时间:
复制 MD 格式

This topic describes how to install Alibaba Cloud SDK V1.0 for Go and call API operations in your project.

Important

Support for Alibaba Cloud SDK V1.0 for Go has ended. For details, see End of support for Alibaba Cloud SDK V1.0 for Golang on March 1, 2025. Use Alibaba Cloud SDK V2.0 for Go instead.

Prerequisites

Before you begin, ensure that you have:

  • Go 1.10.x or later installed. To check your Go version, run:

go version

Install the SDK

Installing a cloud service SDK automatically installs the core library. You only need to install the service-specific SDK.

Cloud service SDK

The cloud service SDK provides request and response objects for calling API operations. The following example installs the Elastic Compute Service (ECS) SDK.

Note

If your project does not have a go.mod file, initialize a Go module first:

go mod init <your-module-name>

Run the following command in your terminal:

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

SDK packages follow the naming format: github.com/aliyun/alibaba-cloud-sdk-go/services/${service-code}.

Core library

The core library includes the client object, signature logic, and error handling. Install it separately only if you need to make generic API calls:

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

Use the SDK

The following example calls the DescribeInstances API operation of ECS in three steps: initialize a client, create a request object, and send the request.

Step 1: Initialize a client

All API calls go through the client object. Initialize the client before making any API call. This example uses an AccessKey pair for authentication. For other credential options, see Manage access credentials.

Note

This example reads the AccessKey pair from the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. Set these variables before running the code. For instructions, see Configure environment variables in Linux, macOS, and Windows. Do not hardcode your credentials in source code.

import (
	"os"

	"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"
)

func main() {
	config := sdk.NewConfig()
	credential, err := credentials.NewStaticAKCredentialsProviderBuilder().
		WithAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")).
		WithAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")).
		Build()
	if err != nil {
		panic(err)
	}
	// Initialize the ECS client for the cn-hangzhou region
	ecsClient, err := ecs.NewClientWithOptions("cn-hangzhou", config, credential)
	if err != nil {
		panic(err)
	}
}

Step 2: Create a request object

Use the SDK's request object to set API parameters.

Note

Name the request object of the API operation in the following format: <API operation name>Request.

// Add "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" to your imports.

// Create a DescribeInstances request object.
request := ecs.CreateDescribeInstancesRequest()
request.InstanceIds = "[\"i-bp1dXXXXXXXXXXXX\"]"
request.PageSize = requests.Integer("100")
request.PageNumber = requests.Integer("1")

Step 3: Send the request

Pass the request object to the client method that corresponds to the API operation. The client uses the credentials and configuration from Step 1.

response, err := ecsClient.DescribeInstances(request)
if err != nil {
	panic(err)
}
fmt.Print(response.GetHttpContentString())

Complete sample code

package main

import (
	"fmt"
	"os"

	"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/sdk/requests"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)

func main() {
	config := sdk.NewConfig()
	credential, err := credentials.NewStaticAKCredentialsProviderBuilder().
		WithAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")).
		WithAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")).
		Build()
	if err != nil {
		panic(err)
	}

	ecsClient, err := ecs.NewClientWithOptions("cn-hangzhou", config, credential)
	if err != nil {
		panic(err)
	}

	// Create a request object.
	request := ecs.CreateDescribeInstancesRequest()
	request.InstanceIds = "[\"i-bp1dXXXXXXXXXXXX\"]"
	request.PageSize = requests.Integer("100")
	request.PageNumber = requests.Integer("1")

	response, err := ecsClient.DescribeInstances(request)
	if err != nil {
		panic(err)
	}
	fmt.Print(response.GetHttpContentString())
}

References