All Products
Search
Document Center

Alibaba Cloud SDK:Configure a proxy

Last Updated:Jul 09, 2024

This topic describes how to configure a proxy in Alibaba Cloud SDK V2.0.

Configuration method

Note

The proxy configurations take effect in the following descending order: the configuration by using the RuntimeOptions object and the configuration when you initialize an SDK client.

  • The following code provides examples on how to configure a proxy by using the RuntimeOptions object.

    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({
        // Obtain the AccessKey ID of the Resource Access Management (RAM) user from an environment variable.
        accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
        // Obtain the AccessKey secret of the RAM user from an environment variable.
        accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
        // Specify the region that you want to access.
        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',
      });
      // Create a RuntimeOptions instance and specify runtime parameters. 
      const runtime = new RuntimeOptions({
        // Configure an HTTP proxy.
        httpProxy: "http://xx.xx.xx.xx:xxxx",
        // Configure an HTTPS proxy.
        httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999",
        // Configure the addresses that do not require proxies.
        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({
                // Obtain the AccessKey ID of the RAM user from an environment variable.
                accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
                // Obtain the AccessKey secret of the RAM user from an environment variable.
                accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
                // Specify the region that you want to access.
                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',
            });
            // Create a RuntimeOptions instance and specify runtime parameters. 
            const runtime = new $Util.RuntimeOptions({
                // Configure an HTTP proxy.
                httpProxy: "http://xx.xx.xx.xx:xxxx",
                // Configure an HTTPS proxy.
                httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999",
                // Configure the addresses that do not require proxies.
                noProxy: '127.0.0.1,localhost'
            });
            const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
            console.log(resp.headers);
            console.log(resp.body);
        }
    }
  • The following code provides examples on how to configure a proxy by using the Config object when you initialize an SDK 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({
        // Obtain the AccessKey ID of the RAM user from an environment variable.
        accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
        // Obtain the AccessKey secret of the RAM user from an environment variable.
        accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
        // Specify the region that you want to access.
        regionId: 'cn-beijing',
        // Configure an HTTP proxy.
        httpProxy: "http://xx.xx.xx.xx:xxxx",
        // Configure an HTTPS proxy.
        httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999",
        // Configure the addresses that do not require proxies.
        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',
      });
      // Create a RuntimeOptions instance and specify runtime parameters. 
      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({
                // Obtain the AccessKey ID of the RAM user from an environment variable.
                accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
                // Obtain the AccessKey secret of the RAM user from an environment variable.
                accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
                // Specify the region that you want to access.
                regionId: 'cn-beijing',
                // Configure an HTTP proxy.
                httpProxy: "http://xx.xx.xx.xx:xxxx",
                // Configure an HTTPS proxy.
                httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999",
                // Configure the addresses that do not require proxies.
                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',
            });
            // Create a RuntimeOptions instance and specify runtime parameters. 
            const runtime = new $Util.RuntimeOptions();
            const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
            console.log(resp.headers);
            console.log(resp.body);
        }
    }