代理配置
更新時(shí)間:
本文為您介紹V2.0 Go SDK對于代理的配置方式。
V2.0 SDK代理配置方式
說明
代理配置優(yōu)先級:RuntimeOption配置->Client配置,優(yōu)先級依次降低。
通過運(yùn)行時(shí)參數(shù)(RuntimeOptions)配置當(dāng)前請求的代理。
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{ // 從環(huán)境變量獲取RAM用戶的AccessKey ID AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), // 從環(huán)境變量獲取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{} // 創(chuàng)建RuntimeObject實(shí)例并設(shè)置運(yùn)行參數(shù)。 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{ // 從環(huán)境變量獲取RAM用戶的AccessKey ID AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), // 從環(huán)境變量獲取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{} // 創(chuàng)建RuntimeObject實(shí)例并設(shè)置運(yùn)行參數(shù)。 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)) }
文檔內(nèi)容是否對您有幫助?