An endpoint is the domain name of an Alibaba Cloud service API, such as ecs.cn-hangzhou.aliyuncs.com. Endpoints vary by service and region. This topic describes how to configure endpoints in Alibaba Cloud SDK V2.0 for Go.
Configure the API request endpoint
SDK V2.0 provides two endpoint configuration methods, listed by priority (highest first).
-
Method 1: Specify an endpoint (recommended)
The V2.0 SDK provides two ways to set an endpoint: specify an endpoint, or specify a region ID to obtain the endpoint automatically.
func main() { config := &openapi.Config{ // Omit the credential configuration. // The endpoint that you want to access. Endpoint: tea.String("<endpoint>"), } client, _err = ecs20140526.NewClient(config) } -
Method 2: Specify a region ID
If you do not specify an endpoint, you can specify a region ID when you initialize the client object of a cloud service. The SDK then resolves the corresponding endpoint automatically.
func main() { config := &openapi.Config{ // Omit the credential configuration. // The region that you want to access. RegionId: tea.String("<RegionId>"), } client, _err := ecs20140526.NewClient(config) }
Configure the authentication endpoint
For some OpenAPI operations that upload files, you can configure a VPC endpoint for the file upload authorization code when you initialize the request client. This makes the network more stable. If you do not configure a VPC endpoint, the default public service address is used, for which global acceleration is already enabled.
-
Configure a VPC endpoint to authenticate file uploads over a VPC.
// Omit the credential configuration package main. import ( "encoding/json" "fmt" "os" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" facebody20191230 "github.com/alibabacloud-go/facebody-20191230/v4/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" ) func main() { config := &openapi.Config{ // Omit the credential configuration. // The region that you want to access. RegionId: tea.String("<RegionId>"), // Configure the VPC endpoint based on the region ID. OpenPlatformEndpoint: tea.String("openplatform-vpc.cn-shanghai.aliyuncs.com"), } client, err := facebody20191230.NewClient(config) if err != nil { panic(err) } f, err := os.Open("The directory of the file that you want to upload in your computer") if err != nil { panic(err) } request := &facebody20191230.DetectBodyCountAdvanceRequest{} request.SetImageURLObject(f) // Create a RuntimeOptions instance and specify runtime parameters. runtime := &util.RuntimeOptions{} runtime.ReadTimeout = tea.Int(10000) resp, err := client.DetectBodyCountAdvance(request, runtime) if err != nil { panic(err) } // The response, which contains the body and headers that are returned by the server side. body, err := json.Marshal(resp.Body) if err != nil { panic(err) } headers, err := json.Marshal(resp.Headers) if err != nil { panic(err) } fmt.Printf("body: %s\n", string(body)) fmt.Printf("header: %s\n", string(headers)) } -
Configure a VPC endpoint for authentication and an internal OSS endpoint for file uploads over a VPC or classic network.
package main import ( "encoding/json" "fmt" "os" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" facebody20191230 "github.com/alibabacloud-go/facebody-20191230/v4/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" ) func main() { config := &openapi.Config{ // Omit the credential configuration. // The region that you want to access. RegionId: tea.String("<RegionId>"), // Configure the VPC endpoint based on the region ID. OpenPlatformEndpoint: tea.String("openplatform-vpc.cn-shanghai.aliyuncs.com"), // Configure the OSS endpoint that you want to use to upload the file. If you set the endpoint type to internal, you can use an internal endpoint to upload the file to OSS over a VPC or the classic network. If you set the endpoint type to accelerate, you can use an accelerated endpoint outside the Chinese mainland to upload the file to OSS. EndpointType: tea.String("internal"), } client, err := facebody20191230.NewClient(config) if err != nil { panic(err) } f, err := os.Open("The directory of the file that you want to upload in your computer") if err != nil { panic(err) } request := &facebody20191230.DetectBodyCountAdvanceRequest{} request.SetImageURLObject(f) // Create a RuntimeOptions instance and specify runtime parameters. runtime := &util.RuntimeOptions{} runtime.ReadTimeout = tea.Int(10000) resp, err := client.DetectBodyCountAdvance(request, runtime) if err != nil { panic(err) } // The response, which contains the body and headers that are returned by the server side. body, err := json.Marshal(resp.Body) if err != nil { panic(err) } headers, err := json.Marshal(resp.Headers) if err != nil { panic(err) } fmt.Printf("body: %s\n", string(body)) fmt.Printf("header: %s\n", string(headers)) }
Appendix: Find an endpoint
You can find the endpoint of a cloud service in the OpenAPI portal:
-
From the top menu of the portal, choose Products and select a product, such as ECS.
-
On the product homepage, find the list of service regions.
-
Select the endpoint that corresponds to your region ID, and then copy and paste it.
Alternatively, on the API debugging page, click Regions in the left-side navigation pane to view the endpoints supported by the cloud service.