[Feature] OSS 上传/下载支持 timeout 配置
- Dominant language
- Python
- Stars
- 485
- Forks
- 81
- Avg merge
- 16h 12m
- Merged PRs (30d)
- 8
Description
**Feature Category**
- [ ] Sandbox
- [ ] Actions
- [ ] Deployments
- [x] SDK & API
- [ ] Envhub
- [ ] CLI
- [ ] Performance & Optimization
- [ ] Documentation & Examples
**Problem Statement**
使用 OSS 模式上传/下载文件时,当文件较大或网络较慢时,可能触发 oss2 SDK 的默认超时,导致传输失败。当前 `_setup_oss()` 方法初始化 OSS 客户端时没有设置 timeout 参数,无法根据实际需求调整超时时间。
```python
# 当前代码缺少 timeout 配置
self._oss_bucket = oss2.Bucket(
auth=auth,
endpoint=env_vars.ROCK_OSS_BUCKET_ENDPOINT,
bucket_name=env_vars.ROCK_OSS_BUCKET_NAME,
region=env_vars.ROCK_OSS_BUCKET_REGION,
# 缺少 timeout 配置!
)
```
**Proposed Solution**
同时支持**环境变量**和**函数参数**两种方式配置 timeout,优先级为:函数参数 > 环境变量 > SDK 默认值(300000ms = 5分钟)
**Detailed Feature Description**
### 1. 环境变量支持
在 `rock/env_vars.py` 中添加:
```python
"ROCK_OSS_TIMEOUT": lambda: int(os.getenv("ROCK_OSS_TIMEOUT", "300000")), # 默认 5 分钟
```
### 2. 修改 `_setup_oss()` 方法
```python
async def _setup_oss(self, timeout: int | None = None) -> OssSetupResponse:
try:
credentials = await self._get_oss_sts_credentials()
auth = oss2.StsAuth(
credentials["AccessKeyId"],
credentials["AccessKeySecret"],
credentials["SecurityToken"],
)
# 优先级:参数 > 环境变量 > 默认值
oss_timeout = timeout or env_vars.ROCK_OSS_TIMEOUT
self._oss_bucket = oss2.Bucket(
auth=auth,
endpoint=env_vars.ROCK_OSS_BUCKET_ENDPOINT,
bucket_name=env_vars.ROCK_OSS_BUCKET_NAME,
region=env_vars.ROCK_OSS_BUCKET_REGION,
connect_timeout=oss_timeout / 1000, # oss2 使用秒为单位
)
except Exception as e:
return OssSetupResponse(success=False, message=f"Failed to setup oss bucket: {e}")
return OssSetupResponse(success=True, message="Successfully setup oss bucket")
```
### 3. 公开方法支持 timeout 参数
```python
# upload_by_path
async def upload_by_path(
self,
source_path: str,
target_path: str,
upload_mode: UploadMode = "auto",
timeout: int | None = None, # 新增
) -> UploadResponse
# download_file
async def download_file(
self,
remote_path: str,
local_path: str,
timeout: int | None = None, # 新增
) -> DownloadFileResponse
```
### 4. 使用示例
```python
from rock.sdk import Sandbox
# 方式 1:环境变量
# export ROCK_OSS_TIMEOUT=600000
async with Sandbox() as sandbox:
# 方式 2:函数参数
await sandbox.upload_by_path(
"/local/file.pdf",
"/remote/file.pdf",
"oss",
timeout=600000 # 10 分钟
)
```
### 5. 参考
TypeScript SDK 已在 v1.3.8 版本中实现此功能,可作为参考实现。
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with rock/env_vars.py and locate _setup_oss(), upload_by_path(), and download_file(). Trace how OSS setup and these public methods currently pass configuration, then implement the documented timeout precedence and unit conversion. Done means both environment-variable and per-call timeout options work for OSS transfers while retaining the SDK default.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, cloud
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100