|
|
há 6 anos atrás | |
|---|---|---|
| integration | há 6 anos atrás | |
| sdk | há 6 anos atrás | |
| services | há 6 anos atrás | |
| vendor | há 7 anos atrás | |
| .gitignore | há 7 anos atrás | |
| .travis.yml | há 6 anos atrás | |
| ChangeLog.txt | há 6 anos atrás | |
| Gopkg.lock | há 7 anos atrás | |
| Gopkg.toml | há 7 anos atrás | |
| LICENSE | há 8 anos atrás | |
| Makefile | há 7 anos atrás | |
| README.md | há 6 anos atrás | |
| README_zh.md | há 6 anos atrás | |
| appveyor.yml | há 6 anos atrás |
See 中文文档
The Alibaba Cloud Go Software Development Kit (SDK) allows you to access Alibaba Cloud services such as Elastic Compute Service (ECS), Server Load Balancer (SLB), and CloudMonitor. You can access Alibaba Cloud services without the need to handle API related tasks, such as signing and constructing your requests.
This document introduces how to obtain and call Alibaba Cloud Go SDK.
If you have any problem while using Go SDK, please submit an issue.
API Explorer provides the ability to call the cloud product OpenAPI online, and dynamically generate SDK Example code and quick retrieval interface, which can significantly reduce the difficulty of using the cloud API. It is highly recommended.
To use Alibaba Cloud Go SDK, you must have an Alibaba Cloud account as well as an AccessKey.
The AccessKey is required when initializing AcsClient. You can create an AccessKey in the Alibaba Cloud console. For more information, see Create an AccessKey.
Note: To increase the security of your account, we recommend that you use the AccessKey of the RAM user to access Alibaba Cloud services.
To use Alibaba Cloud Go SDK to access the APIs of a product, you must first activate the product on the Alibaba Cloud console if required.
Use go get to install SDK:
$ go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdk
If you have used glide to manage dependence, you can also use glide to install Alibaba Cloud Go SDK:
glide get github.com/aliyun/alibaba-cloud-sdk-go
The following code example shows the three main steps to use Alibaba Cloud Go SDK:
package main
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)
func main() {
// Create an ECS client
ecsClient, err := ecs.NewClientWithAccessKey(
"<your-region-id>", // Your Region ID
"<your-access-key-id>", // Your AccessKey ID
"<your-access-key-secret>") // Your AccessKey Secret
if err != nil {
// Handle exceptions
panic(err)
}
// Create an API request and set parameters
request := ecs.CreateDescribeInstancesRequest()
// Set the request.PageSize to "10"
request.PageSize = requests.NewInteger(10)
// Initiate the request and handle exceptions
response, err := ecsClient.DescribeInstances(request)
if err != nil {
// Handle exceptions
panic(err)
}
fmt.Println(response)
}
When you create an instance of client, you need to fill out three parameters: Region ID、Access Key ID and Access Key Secret. You can get Access Key ID and Access Key Secret from console, and get Region ID from region list
Alibaba Cloud Go SDK uses primordial net/http of Go language to send and accept requests,so it's configuration is the same as net/http's,you can use config to deliver configuration to the bottomed httpClient.
httpTransport := http.Transport{
// set http client options
}
config := sdk.NewConfig()
.WithHttpTransport(&httpTransport)
.WithTimeout(timeout)
ecsClient, err := ecs.NewClientWithOptions(config)
// Maximum Running Vusers
poolSize := 2
// The maximum number of requests that can be cached
maxTaskQueueSize := 5
// Enable asynchronous functionality at creation time
config := sdk.NewConfig()
.WithEnableAsync(true)
.WithGoRoutinePoolSize(poolSize) // Optional,default:5
.WithMaxTaskQueueSize(maxTaskQueueSize) // Optional,default:1000
ecsClient, err := ecs.NewClientWithOptions(config)
// It can also be opened after client is initialized
client.EnableAsync(poolSize, maxTaskQueueSize)
Alibaba Cloud Go SDK supports asynchronous calls in two ways:
Using channel as return values
responseChannel, errChannel := client.FooWithChan(request)
// this will block
response := <-responseChannel
err = <-errChannel
blocker := client.FooWithCallback(request, func(response *FooResponse, err error) {
// handle the response and err
})
// blocker which is type of (chan int),is used to control synchronization,when returning 1 means success,and returning 0 means failure.
// When <-blocker returns failure,err also will be handled by afferent callback.
result := <-blocker
CommonAPI which is launched by Alibaba Cloud Go SDK,is an universal grid way to call API. CommonAPI has the following characteristics: :
CommonAPI needs to be used in conjunction with the corresponding API documentation to query information about the API.
You can query the API documentation for all products atDocument Center.
Launching a CommonAPI request requires you to query the following parameters:
We take ECS DescribeInstanceStatus API as an example.
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)
func main() {
client, err := sdk.NewClientWithAccessKey("cn-hangzhou", os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
if err != nil {
panic(err)
}
request := requests.NewCommonRequest()
request.Domain = "ecs.aliyuncs.com"
request.Version = "2014-05-26"
request.ApiName = "DescribeInstanceStatus"
request.QueryParams["PageNumber"] = "1"
request.QueryParams["PageSize"] = "30"
response, err := client.ProcessCommonRequest(request)
if err != nil {
panic(err)
}
fmt.Print(response.GetHttpContentString())
}