Skip to content

Commit 4558e45

Browse files
author
BitsAdmin
committed
Merge branch 'dev/lsx/http-proxy' into 'integration_2025-07-24_1018981540354'
feat: [development task] core (1482850) See merge request iaasng/volcengine-python-sdk!721
2 parents 15c528a + 525a889 commit 4558e45

File tree

3 files changed

+134
-29
lines changed

3 files changed

+134
-29
lines changed

SDK_Integration_zh.md

Lines changed: 106 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
# 目录
2+
- [目录](#目录)
23
- [集成SDK](#集成sdk)
3-
- [环境要求](#环境要求)
4-
- [访问凭据](#访问凭据)
5-
- [AK、SK设置](#aksk设置)
6-
- [EndPoint配置](#endpoint配置)
7-
- [自定义Endpoint](#自定义endpoint)
8-
- [自定义RegionId](#自定义regionid)
9-
- [自动化Endpoint寻址](#自动化endpoint寻址)
10-
- [Endpoint默认寻址](#endpoint默认寻址)
11-
- [Http连接池配置](#http连接池配置)
12-
- [Https请求配置](#https请求配置)
13-
- [指定scheme](#指定scheme)
14-
- [忽略SSL验证](#忽略ssl验证)
15-
- [超时配置](#超时配置)
16-
- [重试机制](#重试机制)
17-
- [重试代码配置](#重试代码配置)
18-
- [重试条件](#重试条件)
19-
- [默认重试条件](#默认重试条件)
20-
- [自定义重试条件](#自定义重试条件)
21-
- [退避策略](#退避策略)
22-
- [内置退避策略](#内置退避策略)
23-
- [自定义退避策略](#自定义退避策略)
24-
- [异常处理](#异常处理)
25-
- [Debug机制](#debug机制)
26-
- [指定日志Logger](#指定日志logger)
4+
- [环境要求](#环境要求)
5+
- [访问凭据](#访问凭据)
6+
- [AK、SK设置](#aksk设置)
7+
- [STS Token设置](#sts-token设置)
8+
- [EndPoint配置](#endpoint配置)
9+
- [自定义Endpoint](#自定义endpoint)
10+
- [自定义RegionId](#自定义regionid)
11+
- [自动化Endpoint寻址](#自动化endpoint寻址)
12+
- [Endpoint默认寻址](#endpoint默认寻址)
13+
- [Http连接池配置](#http连接池配置)
14+
- [Https请求配置](#https请求配置)
15+
- [指定scheme](#指定scheme)
16+
- [Http(s)代理配置](#https代理配置)
17+
- [配置Http(s)代理](#配置https代理)
18+
- [注意事项](#注意事项)
19+
- [忽略SSL验证](#忽略ssl验证)
20+
- [超时配置](#超时配置)
21+
- [重试机制](#重试机制)
22+
- [重试代码配置](#重试代码配置)
23+
- [重试条件](#重试条件)
24+
- [默认重试条件](#默认重试条件)
25+
- [自定义重试条件](#自定义重试条件)
26+
- [退避策略](#退避策略)
27+
- [内置退避策略](#内置退避策略)
28+
- [自定义退避策略](#自定义退避策略)
29+
- [异常处理](#异常处理)
30+
- [Debug机制](#debug机制)
31+
- [指定日志Logger](#指定日志logger)
2732

2833
# 集成SDK
2934

@@ -36,7 +41,7 @@
3641

3742
# 访问凭据
3843

39-
为保障资源访问安全,火山引擎 Python SDK 目前暂时只支持 **AK/SK**认证设置。
44+
为保障资源访问安全,火山引擎 Python SDK 目前支持 `AK/SK``STS Token` 认证设置。
4045

4146
## AK、SK设置
4247

@@ -83,6 +88,53 @@ except ApiException as e:
8388
pass
8489
```
8590

91+
## STS Token设置
92+
93+
STS(Security Token Service)是火山引擎提供的临时访问凭证机制。开发者通过服务端调用 STS 接口获取临时凭证(临时 AK、SK 和 Token),有效期可配置,适用于安全要求较高的场景。
94+
95+
> ⚠️ 注意事项
96+
>
97+
> 1. 最小权限: 仅授予调用方访问所需资源的最小权限,避免使用 * 通配符授予全资源、全操作权限。
98+
> 2. 设置合理的有效期: 请根据实际情况设置合理有效期,越短越安全,建议不要超过1小时。
99+
100+
支持`configuration`级别全局配置和接口级别的运行时参数设置`RuntimeOption`;`RuntimeOption`设置会覆盖`configuration`全局配置。
101+
102+
**代码示例:**
103+
```python
104+
import volcenginesdkcore,volcenginesdkecs
105+
from volcenginesdkcore.rest import ApiException
106+
from volcenginesdkcore.interceptor import RuntimeOption
107+
108+
# 全局设置
109+
configuration = volcenginesdkcore.Configuration()
110+
configuration.ak = "Your ak"
111+
configuration.sk = "Your sk"
112+
configuration.session_token = "Your session token"
113+
configuration.debug = True
114+
volcenginesdkcore.Configuration.set_default(configuration)
115+
116+
# 接口级别运行时参数设置,会覆盖全局配置
117+
runtime_options = RuntimeOption(
118+
ak = "Your ak",
119+
sk = "Your sk",
120+
session_token="Your session token",
121+
client_side_validation = True, # 开启客户端校验,默认开启
122+
)
123+
api_instance = volcenginesdkecs.ECSApi()
124+
create_command_request = volcenginesdkecs.CreateCommandRequest(
125+
command_content="ls -l",
126+
description="Your command description",
127+
name="Your command name",
128+
type="command",
129+
_configuration=runtime_options, # 配置运行时参数
130+
)
131+
try:
132+
api_instance.create_command(create_command_request)
133+
except ApiException as e:
134+
pass
135+
```
136+
137+
86138

87139
# EndPoint配置
88140

@@ -233,6 +285,35 @@ except ApiException as e:
233285
pass
234286
```
235287

288+
# Http(s)代理配置
289+
290+
> - **默认**
291+
> 无代理
292+
293+
## 配置Http(s)代理
294+
295+
```python
296+
configuration = volcenginesdkcore.Configuration()
297+
configuration.ak = "Your AK"
298+
configuration.sk = "Your SK"
299+
300+
configuration.http_proxy = "http://your_proxy:8080"
301+
configuration.https_proxy = "http://your_proxy:8080"
302+
303+
volcenginesdkcore.Configuration.set_default(configuration)
304+
305+
api_instance = volcenginesdkecs.ECSApi()
306+
```
307+
308+
## 注意事项
309+
310+
支持通过以下环境变量配置代理:
311+
312+
http_proxy/HTTP_PROXY, https_proxy/HTTPS_PROXY, no_proxy/NO_PROXY
313+
314+
优先级:代码 > 环境变量
315+
316+
236317
## 忽略SSL验证
237318

238319
> **默认**

volcenginesdkcore/configuration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ def __init__(self):
127127

128128
# Proxy URL
129129
self.proxy = None
130+
self.http_proxy = None
131+
self.https_proxy = None
132+
130133
# Safe chars for path_param
131134
self.safe_chars_for_path_param = ''
132135

volcenginesdkcore/rest.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io
66
import json
77
import logging
8+
import os
89
import re
910
import ssl
1011

@@ -38,7 +39,6 @@ def getheader(self, name, default=None):
3839
"""Returns a given response header."""
3940
return self.urllib3_response.getheader(name, default)
4041

41-
4242
class RESTClientObject(object):
4343

4444
def __init__(self, configuration, pools_size=4, maxsize=None):
@@ -48,6 +48,8 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
4848
# maxsize is the number of requests to host that are allowed in parallel # noqa: E501
4949
# Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html # noqa: E501
5050

51+
proxy_url = self.__get_proxy(configuration)
52+
5153
# cert_reqs
5254
if configuration.verify_ssl:
5355
cert_reqs = ssl.CERT_REQUIRED
@@ -78,16 +80,16 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
7880
connect=configuration.connect_timeout,
7981
read=configuration.read_timeout,
8082
)
81-
# https pool manager
82-
if configuration.proxy:
83+
84+
if proxy_url:
8385
self.pool_manager = urllib3.ProxyManager(
8486
num_pools=pools_size,
8587
maxsize=maxsize,
8688
cert_reqs=cert_reqs,
8789
ca_certs=ca_certs,
8890
cert_file=configuration.cert_file,
8991
key_file=configuration.key_file,
90-
proxy_url=configuration.proxy,
92+
proxy_url=proxy_url,
9193
timeout=timeout,
9294
retries=Retry(total=False),
9395
**addition_pool_args
@@ -105,6 +107,25 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
105107
**addition_pool_args
106108
)
107109

110+
def __get_proxy(self, configuration):
111+
proxy_url = configuration.proxy
112+
if not proxy_url:
113+
if configuration.scheme == 'http':
114+
if configuration.http_proxy:
115+
proxy_url = configuration.http_proxy
116+
elif os.getenv('HTTP_PROXY'):
117+
proxy_url = os.getenv('HTTP_PROXY')
118+
elif os.getenv('http_proxy'):
119+
proxy_url = os.getenv('http_proxy')
120+
elif configuration.scheme == 'https':
121+
if configuration.https_proxy:
122+
proxy_url = configuration.https_proxy
123+
elif os.getenv('HTTPS_PROXY'):
124+
proxy_url = os.getenv('HTTPS_PROXY')
125+
elif os.getenv('https_proxy'):
126+
proxy_url = os.getenv('https_proxy')
127+
return proxy_url
128+
108129
def request(self, method, url, query_params=None, headers=None,
109130
body=None, post_params=None, _preload_content=True,
110131
_request_timeout=None):

0 commit comments

Comments
 (0)