代理配置
更新時(shí)間:
本節(jié)主要介紹V2.0 SDK對(duì)于代理的配置。
代理配置方式
說(shuō)明
代理配置優(yōu)先級(jí):RuntimeOption配置->Client配置,優(yōu)先級(jí)依次降低。
通過(guò)運(yùn)行時(shí)參數(shù)(RuntimeOptions)配置當(dāng)前請(qǐng)求的代理。
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({ // 從環(huán)境變量中獲取RAM用戶的AccessKey ID accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID, // 從環(huán)境變量中獲取RAM用戶的AccessKey Secret accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET, // 訪問(wèn)的區(qū)域 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', }); // 創(chuàng)建RuntimeObject實(shí)例并設(shè)置運(yùn)行參數(shù)。 const runtime = new RuntimeOptions({ // 設(shè)置http代理 httpProxy: "http://xx.xx.xx.xx:xxxx", // 設(shè)置https代理 httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999", // 設(shè)置非代理地址 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({ // 從環(huán)境變量中獲取RAM用戶的AccessKey ID accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID, // 從環(huán)境變量中獲取RAM用戶的AccessKey Secret accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET, // 訪問(wèn)的區(qū)域 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', }); // 創(chuàng)建RuntimeObject實(shí)例并設(shè)置運(yùn)行參數(shù)。 const runtime = new $Util.RuntimeOptions({ // 設(shè)置http代理 httpProxy: "http://xx.xx.xx.xx:xxxx", // 設(shè)置https代理 httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999", // 設(shè)置非代理地址 noProxy: '127.0.0.1,localhost' }); const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime); console.log(resp.headers); console.log(resp.body); } }
通過(guò)Config在初始化Client階段配置請(qǐng)求的代理。
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({ // 從環(huán)境變量中獲取RAM用戶的AccessKey ID accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID, // 從環(huán)境變量中獲取RAM用戶的AccessKey Secret accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET, // 訪問(wèn)的區(qū)域 regionId: 'cn-beijing', // 設(shè)置http代理 httpProxy: "http://xx.xx.xx.xx:xxxx", // 設(shè)置https代理 httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999", // 設(shè)置非代理地址 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', }); // 創(chuàng)建RuntimeObject實(shí)例并設(shè)置運(yùn)行參數(shù)。 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({ // 從環(huán)境變量中獲取RAM用戶的AccessKey ID accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID, // 從環(huán)境變量中獲取RAM用戶的AccessKey Secret accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET, // 訪問(wèn)的區(qū)域 regionId: 'cn-beijing', // 設(shè)置http代理 httpProxy: "http://xx.xx.xx.xx:xxxx", // 設(shè)置https代理 httpsProxy: "https://username:password@xxx.xxx.xxx.xxx:9999", // 設(shè)置非代理地址 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', }); // 創(chuàng)建RuntimeObject實(shí)例并設(shè)置運(yùn)行參數(shù)。 const runtime = new $Util.RuntimeOptions(); const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime); console.log(resp.headers); console.log(resp.body); } }
文檔內(nèi)容是否對(duì)您有幫助?