Ortamınız giden trafiği bir proxy sunucusu üzerinden yönlendiriyorsa SDK isteklerini göndermeden önce proxy'yi yapılandırın.
Yapılandırma yöntemleri
Her iki yöntem de yapılandırıldığında istemci başlatma ayarı ortam değişkenlerine göre önceliklidir.
-
Ortam değişkenleri — Konteynerleştirilmiş veya CI/CD ortamları için daha basit bir yaklaşımdır. Uygulamanızı başlatmadan önce aşağıdaki değişkenlerden birini veya birkaçını ayarlayın:
HTTP_PROXYveyahttp_proxy: HTTP istekleri için proxy URL'si. Örnek:http://127.0.0.1:8080.HTTPS_PROXYveyahttps_proxy: İsteğe bağlı kimlik bilgileri içeren HTTPS istekleri için proxy URL'si. Biçim:http://<user>:<password>@<host>:<port>. Örnek:http://user:password@127.0.0.1:8989.NO_PROXYveyano_proxy: Proxy'yi atlayan IP adreslerinin veya alan adlarının virgülle ayrılmış listesi. Örnek:127.0.0.1,localhost.
-
İstemci başlatma — Bir SDK istemcisini başlatırken proxy'yi programatik olarak yapılandırın. Bu ayar ortam değişkeni yapılandırmalarını geçersiz kılar.
import ( "fmt" "os" "github.com/aliyun/alibaba-cloud-sdk-go/sdk" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials" ecs "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" ) func main() { // Initialize the SDK configuration. config := sdk.NewConfig() // Use the AccessKey ID and AccessKey secret of the Resource Access Management (RAM) user. credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")) client, err := ecs.NewClientWithOptions("cn-hangzhou", config, credential) if err != nil { panic(err) } // Configure proxies on the initialized client. // Format for an authenticated proxy: http://<user>:<password>@<host>:<port> client.SetHttpsProxy("http://user:password@127.0.0.1:8989") // HTTPS proxy client.SetHttpProxy("http://127.0.0.1:8080") // HTTP proxy client.SetNoProxy("127.0.0.1,localhost") // Comma-separated IPs or domain names that bypass the proxy // Create a request. request := ecs.CreateDescribeRegionsRequest() request.Scheme = "https" // Set the request parameters. request.InstanceChargeType = "PrePaid" // The billing method of the instance. request.ResourceType = "instance" // The resource type. // Send the request and handle the response. response, err := client.DescribeRegions(request) if err != nil { fmt.Print(err.Error()) } fmt.Printf("response is %#v\n", response) }