Configure an endpoint
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).
-
Specify a custom endpoint: Set the endpoint when you initialize the client. Find service endpoints on the product page in OpenAPI Explorer, or follow the steps in the appendix.
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) } -
Specify a region ID:
-
If the SDK contains a region-to-endpoint map for the service and the specified region ID exists in it, the SDK retrieves the corresponding endpoint.
-
If the map is missing or does not contain the specified region ID, the SDK constructs the endpoint automatically using a naming convention.
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) } -
VPC endpoint use cases
Use a VPC endpoint in these scenarios:
-
Your business runs in both on-premises data centers and the cloud, with VPC-isolated modules connected over the public internet.
-
You connect a VPC-based cloud data center to your on-premises environment through Express Connect for hybrid cloud scenarios such as data security, burst capacity, and fast data synchronization.
-
Multiple applications in a VPC with different peak hours share bandwidth across IP addresses to smooth traffic and reduce costs.
-
Your services run entirely in VPCs across multiple nodes and require high-speed connections between them for geographically distributed users.
Configure the authentication endpoint
Some API operations accept a custom open platform endpoint. If omitted, the SDK uses the default public endpoint. Internally, the SDK calls a file upload authentication service to obtain upload credentials and default OSS instance information.
-
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
Find service endpoints in OpenAPI Explorer:
-
From the top menu, select Products, and then choose a service such as ECS.
2. On the product homepage, find the list of regions.
3. Select the endpoint for your region ID, then copy and paste it.
Alternatively, on the API Debugging page, click Regions in the left-side navigation pane to view available endpoints.