代理配置

更新时间:

本节主要介绍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",
        // 设置非代理地址
        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",
                // 设置非代理地址
                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",
        // 设置非代理地址
        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",
                // 设置非代理地址
                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);
        }
    }