Todos os produtos
Search
Central de documentação

Alibaba Cloud SDK:Configure um endpoint

Última atualização: Sep 22, 2026

Um endpoint é o nome de domínio de uma API de service da Alibaba Cloud, como ecs.cn-hangzhou.aliyuncs.com. Os endpoints variam de acordo com o service e a região. Este tópico descreve como configurar endpoints no Alibaba Cloud SDK V2.0 para Go.

Configure o endpoint de solicitação da API

O SDK V2.0 oferece dois métodos de configuração de endpoint, listados por prioridade (da maior para a menor).

  1. Método 1: Especifique um endpoint (recomendado)

    O SDK V2.0 oferece duas formas de definir um endpoint: especifique um endpoint diretamente ou um ID de região para obter o endpoint automaticamente.

    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)
    }
  2. Método 2: Especifique um ID de região

    Caso não especifique um endpoint, especifique um ID de região ao inicializar o objeto client de um cloud service. O SDK resolve o endpoint correspondente automaticamente.

    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 o endpoint de autenticação

Para algumas operações do OpenAPI que fazem upload de arquivos, configure um endpoint de VPC para o código de autorização de upload ao inicializar o client da solicitação. Isso torna a rede mais estável. Se não configurar um endpoint de VPC, o SDK usará o endereço público padrão do service, que já possui aceleração global ativada.

  • Configure um endpoint de VPC para autenticar uploads de arquivos por meio de uma 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 um endpoint de VPC para autenticação e um endpoint interno do OSS para uploads de arquivos por meio de uma VPC ou rede clássica.

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

Apêndice: Encontrar um endpoint

Encontre o endpoint de um cloud service no portal do OpenAPI:

  1. No menu superior do portal, escolha Products e selecione um product, como o ECS.

  2. Na product homepage, localize a list of service regions.

  3. Selecione o endpoint correspondente ao seu ID de região e, em seguida, copie e cole o valor.

Como alternativa, na página de depuração da API, clique em Regions no painel de navegação à esquerda para visualizar os endpoints compatíveis com o cloud service.