全部產品
Search
文件中心

Alibaba Cloud SDK:代理配置

更新時間:Jul 01, 2024

本文為您介紹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))
    }