Un endpoint est le nom de domaine d'une API de service Alibaba Cloud, par exemple ecs.cn-hangzhou.aliyuncs.com. Les endpoints varient selon le service et la région. Cette rubrique explique comment configurer les endpoints dans Alibaba Cloud SDK V2.0 pour Go.
Configurer l'endpoint de la requête API
Le SDK V2.0 propose deux méthodes de configuration des endpoints, classées par ordre de priorité (la plus élevée en premier).
-
Méthode 1 : Spécifier un endpoint (recommandé)
Le SDK V2.0 offre deux possibilités pour définir un endpoint : spécifier directement l'endpoint ou indiquer un ID de région afin d'obtenir l'endpoint automatiquement.
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) } -
Méthode 2 : Spécifier un ID de région
Si vous ne spécifiez pas d'endpoint, indiquez un ID de région lors de l'initialisation de l'objet client d'un service cloud. Le SDK résout alors automatiquement l'endpoint correspondant.
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) }
Configurer l'endpoint d'authentification
Pour certaines opérations OpenAPI qui impliquent le téléchargement de fichiers, configurez un endpoint VPC pour le code d'autorisation de téléchargement lors de l'initialisation du client de requête. Cela améliore la stabilité du réseau. Si aucun endpoint VPC n'est configuré, l'adresse de service public par défaut est utilisée ; celle-ci bénéficie déjà de l'accélération globale.
-
Configurez un endpoint VPC pour authentifier les téléchargements de fichiers via un 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)) } -
Configurez un endpoint VPC pour l'authentification et un endpoint OSS interne pour les téléchargements de fichiers via un VPC ou le réseau classique.
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)) }
Annexe : Trouver un endpoint
Trouvez l'endpoint d'un service cloud sur le portail OpenAPI :
Dans le menu supérieur du portail, choisissez Products et sélectionnez un produit, tel que ECS.
Sur la product homepage, repérez la list of service regions.
Sélectionnez l'endpoint correspondant à votre ID de région, puis copiez-le et collez-le.
Vous pouvez également consulter les endpoints pris en charge par le service cloud en cliquant sur Regions dans le volet de navigation de gauche de la page de débogage des API.