If your environment routes outbound traffic through a proxy server, configure the proxy before sending SDK requests.
Configuration methods
When both methods are configured, the client initialization setting takes priority over environment variables.
-
Environment variables — the simpler approach for containerized or CI/CD environments. Set one or more of the following variables before starting your application:
HTTP_PROXYorhttp_proxy: the proxy URL for HTTP requests. Example:http://127.0.0.1:8080.HTTPS_PROXYorhttps_proxy: the proxy URL for HTTPS requests, with optional credentials. Format:http://<user>:<password>@<host>:<port>. Example:http://user:password@127.0.0.1:8989.NO_PROXYorno_proxy: a comma-separated list of IP addresses or domain names that bypass the proxy. Example:127.0.0.1,localhost.
-
Client initialization — configure the proxy programmatically when initializing an SDK client. This setting overrides any environment variable configuration.
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) }