代理配置
更新时间:
本文为您介绍V2.0 Go SDK对于代理的配置方式。
V2.0 SDK代理配置方式
说明
代理配置优先级:RuntimeOption配置->Client配置,优先级依次降低。
通过运行时参数(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{ // 从环境变量获取RAM用户的AccessKey ID AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), // 从环境变量获取RAM用户的AccessKey Secret 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{} // 创建RuntimeObject实例并设置运行参数。 runtime := &util.RuntimeOptions{} // 通过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)) }
通过Config在初始化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{ // 从环境变量获取RAM用户的AccessKey ID AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), // 从环境变量获取RAM用户的AccessKey Secret AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")), // Region ID RegionId: tea.String("<RegionId>"), // 通过Config配置代理 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{} // 创建RuntimeObject实例并设置运行参数。 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)) }
文档内容是否对您有帮助?