全部產品
Search
文件中心

Alibaba Cloud SDK:代理配置

更新時間:Jul 01, 2024

本節主要介紹V2.0 SDK對於代理的配置。

代理配置方式

說明

代理配置優先順序:RuntimeOption配置->Client配置,優先順序依次降低。

  • 通過運行時參數(RuntimeOptions)配置當前請求的代理。

    JavaScript樣本

    const { default: Ecs20140526, ModifySecurityGroupRuleRequest } = require('@alicloud/ecs20140526');
    const { Config } = require('@alicloud/openapi-client');
    const { RuntimeOptions } = require('@alicloud/tea-util');
    
    async function main() {
      const config = new Config({
        // 從環境變數中擷取RAM使用者的AccessKey ID
        accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
        // 從環境變數中擷取RAM使用者的AccessKey Secret
        accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
        // 訪問的地區
        regionId: 'cn-beijing',
      });
      const client = new Ecs20140526(config);
      const request = new ModifySecurityGroupRuleRequest({
        regionId: 'cn-beijing',
        securityGroupId: 'sg-2zec0dm6qi66XXXXXXXX',
        securityGroupRuleId: 'sgr-2zeah9lqneb4XXXXXXXX',
        policy: 'accept',
        ipProtocol: 'tcp',
        portRange: '5004/5004',
      });
      // 建立RuntimeObject執行個體並設定運行參數。
      const runtime = new RuntimeOptions({
        // 設定http代理
        httpProxy: "http://xx.xx.xx.xx:xxxx",
        // 設定https代理
        httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999",
        // 設定非Proxy 位址
        noProxy: '127.0.0.1,localhost',
      });
      const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
      console.log(resp.headers);
      console.log(resp.body);
    }

    TypeScript樣本

    import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
    import * as $OpenApi from '@alicloud/openapi-client';
    import * as $Util from '@alicloud/tea-util';
    
    export default class Client {
        static async main(): Promise<void> {
            const config = new $OpenApi.Config({
                // 從環境變數中擷取RAM使用者的AccessKey ID
                accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
                // 從環境變數中擷取RAM使用者的AccessKey Secret
                accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
                // 訪問的地區
                regionId: 'cn-beijing',
            });
            const client = new Ecs20140526(config);
            const request = new $Ecs20140526.ModifySecurityGroupRuleRequest({
                regionId: 'cn-beijing',
                securityGroupId: 'sg-2zec0dm6qi66XXXXXXXX',
                securityGroupRuleId: 'sgr-2zeah9lqneb4XXXXXXXX',
                policy: 'accept',
                ipProtocol: 'tcp',
                portRange: '5004/5004',
            });
            // 建立RuntimeObject執行個體並設定運行參數。
            const runtime = new $Util.RuntimeOptions({
                // 設定http代理
                httpProxy: "http://xx.xx.xx.xx:xxxx",
                // 設定https代理
                httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999",
                // 設定非Proxy 位址
                noProxy: '127.0.0.1,localhost'
            });
            const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
            console.log(resp.headers);
            console.log(resp.body);
        }
    }
  • 通過Config在初始化Client階段配置請求的代理。

    JavaScript樣本

    const { default: Ecs20140526, ModifySecurityGroupRuleRequest } = require('@alicloud/ecs20140526');
    const { Config } = require('@alicloud/openapi-client');
    const { RuntimeOptions } = require('@alicloud/tea-util');
    
    async function main() {
      const config = new Config({
        // 從環境變數中擷取RAM使用者的AccessKey ID
        accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
        // 從環境變數中擷取RAM使用者的AccessKey Secret
        accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
        // 訪問的地區
        regionId: 'cn-beijing',
        // 設定http代理
        httpProxy: "http://xx.xx.xx.xx:xxxx",
        // 設定https代理
        httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999",
        // 設定非Proxy 位址
        noProxy: '127.0.0.1,localhost',
      });
      const client = new Ecs20140526(config);
      const request = new ModifySecurityGroupRuleRequest({
        regionId: 'cn-beijing',
        securityGroupId: 'sg-2zec0dm6qi66XXXXXXXX',
        securityGroupRuleId: 'sgr-2zec0dm6qi66XXXXXXXX',
        policy: 'accept',
        ipProtocol: 'tcp',
        portRange: '5004/5004',
      });
      // 建立RuntimeObject執行個體並設定運行參數。
      const runtime = new RuntimeOptions();
      const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
      console.log(resp.headers);
      console.log(resp.body);
    }
    

    TypeScript樣本

    import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
    import * as $OpenApi from '@alicloud/openapi-client';
    import * as $Util from '@alicloud/tea-util';
    
    export default class Client {
        static async main(): Promise<void> {
            const config = new $OpenApi.Config({
                // 從環境變數中擷取RAM使用者的AccessKey ID
                accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
                // 從環境變數中擷取RAM使用者的AccessKey Secret
                accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
                // 訪問的地區
                regionId: 'cn-beijing',
                // 設定http代理
                httpProxy: "http://xx.xx.xx.xx:xxxx",
                // 設定https代理
                httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999",
                // 設定非Proxy 位址
                noProxy: '127.0.0.1,localhost'
            });
            const client = new Ecs20140526(config);
            const request = new $Ecs20140526.ModifySecurityGroupRuleRequest({
                regionId: 'cn-beijing',
                securityGroupId: 'sg-2zec0dm6qi66XXXXXXXX',
                securityGroupRuleId: 'sgr-2zec0dm6qi66XXXXXXXX',
                policy: 'accept',
                ipProtocol: 'tcp',
                portRange: '5004/5004',
            });
            // 建立RuntimeObject執行個體並設定運行參數。
            const runtime = new $Util.RuntimeOptions();
            const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
            console.log(resp.headers);
            console.log(resp.body);
        }
    }