Installation
Use go get
to install SDK:
go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdk
Quick Examples
Before you begin, you need to sign up for an Alibaba Cloud account and retrieve your Credentials.
Create Client
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 exceptions
panic(err)
}
}
ROA Request
package main
import "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
func main() {
request := requests.NewCommonRequest() // Make a common request
request.Method = "GET" // Set request method
request.Product = "CS" // Specify product
request.Domain = "cs.aliyuncs.com" // Location Service will not be enabled if the host is specified. For example, service with aCertification type-Bearer Token should be specified
request.Version = "2015-12-15" // Specify product version
request.PathPattern = "/clusters/[ClusterId]" // Specify path rule with ROA-style
request.Scheme = "https" // Set request scheme. Default: http
request.ApiName = "DescribeCluster" // Specify product interface
request.QueryParams["ClusterId"] = "123456" // Assign values to parameters in the path
request.QueryParams["RegionId"] = "region_id" // Specify the requested regionId, if not specified, use the client regionId, then default regionId
request.TransToAcsRequest() // Trans commonrequest to acsRequest, which is used by client.
}
RPC Request
package main
import "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
func main() {
request := requests.NewCommonRequest() // Make a common request
request.Method = "POST" // Set request method
request.Product = "Ecs" // Specify product
request.Domain = "ecs.aliyuncs.com" // Location Service will not be enabled if the host is specified. For example, service with a Certification type-Bearer Token should be specified
request.Version = "2014-05-26" // Specify product version
request.Scheme = "https" // Set request scheme. Default: http
request.ApiName = "CreateInstance" // Specify product interface
request.QueryParams["InstanceType"] = "ecs.g5.large" // Assign values to parameters in the path
request.QueryParams["RegionId"] = "region_id" // Specify the requested regionId, if not specified, use the client regionId, then default regionId
request.TransToAcsRequest() // Trans commonrequest to acsRequest, which is used by client.
}
Demo
package main
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
)
func main(){
client, err := dysmsapi.NewClientWithAccessKey("cn-beijing", "xxxx", "xxx")
if err != nil {
panic(err)
}
request := requests.NewCommonRequest()
request.Method = "POST"
request.Scheme = "https" // https | http
request.Domain = "dysmsapi.aliyuncs.com"
request.Version = "2017-05-25"
request.ApiName = "SendSms"
request.QueryParams["RegionId"] = "cn-beijing"
request.QueryParams["PhoneNumbers"] = "xxx"
request.QueryParams["SignName"] = "xxx"
request.QueryParams["TemplateCode"] = "xxx"
request.QueryParams["TemplateParam"] = "{\"code\":" + "777777" + "}"
response, err := client.ProcessCommonRequest(request)
fmt.Print( client.DoAction(request, response))
// fmt.Print(response)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}