This topic describes how to configure a proxy in Alibaba Cloud SDK V2.0 for Go.
Methods
Note
The priority levels of the methods that are used to configure a proxy are listed in descending order: use RuntimeOptions and use a Config object when you initialize an SDK client.
Configure a proxy by using RuntimeOptions.
package main import ( "encoding/json" "fmt" "os" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v4/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" ) func main() { config := &openapi.Config{ // Obtain the AccessKey ID of the Resource Access Management (RAM) user from environment variables. AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), // Obtain the AccessKey secret of the RAM user from environment variables. AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")), // Region ID RegionId: tea.String("<RegionId>"), } client, _err := ecs20140526.NewClient(config) if _err != nil { panic(_err) } describeRegionsRequest := &ecs20140526.DescribeRegionsRequest{} // Create a RuntimeObject instance and configure runtime parameters. runtime := &util.RuntimeOptions{} // Configure a proxy by using RuntimeOptions. runtime.HttpProxy = tea.String("http://127.0.0.1:9898") runtime.HttpsProxy = tea.String("http://user:password@127.0.0.1:8989") runtime.NoProxy = tea.String("127.0.0.1,localhost") resp, _err := client.DescribeRegionsWithOptions(describeRegionsRequest, runtime) if _err != nil { panic(_err) } body, err := json.Marshal(resp.Body) if err != nil { panic(err) } fmt.Printf("body: %s\n", string(body)) }
Configure a proxy by using a Config object when you initialize an SDK client.
package main import ( "encoding/json" "fmt" "os" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v4/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" ) func main() { config := &openapi.Config{ // Obtain the AccessKey ID of the RAM user from environment variables. AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), // Obtain the AccessKey secret of the RAM user from environment variables. AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")), // Region ID RegionId: tea.String("<RegionId>"), // Configure a proxy by using a Config object. HttpProxy: tea.String("http://127.0.0.1:9898"), HttpsProxy: tea.String("http://user:password@127.0.0.1:8989"), NoProxy: tea.String("127.0.0.1,localhost"), } client, _err := ecs20140526.NewClient(config) if _err != nil { panic(_err) } describeRegionsRequest := &ecs20140526.DescribeRegionsRequest{} // Create a RuntimeObject instance and configure runtime parameters. runtime := &util.RuntimeOptions{} resp, _err := client.DescribeRegionsWithOptions(describeRegionsRequest, runtime) if _err != nil { panic(_err) } body, err := json.Marshal(resp.Body) if err != nil { panic(err) } fmt.Printf("body: %s\n", string(body)) }