video-shuoshan/backend/utils/tos_client.py
seaislee1209 32f0ee58f4 feat: v0.8.0 — Seedance API 全流程修复 + TOS 视频持久化
- 新增音频引用传递给 Seedance API
- 视频生成完成后自动持久化到 TOS(永久 CDN URL)
- 移除 ARK_API_KEY 硬编码默认值
- 前端渐进式轮询(10s/30s/60s)替代固定 3 分钟
- TOS 桶切换到 airdrama-media (cn-beijing)
- K8s Secret 注入 TOS 密钥,CI/CD 同步更新

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 21:26:27 +08:00

72 lines
1.9 KiB
Python

"""Volcano Engine TOS file upload utility using official TOS SDK."""
import uuid
from django.conf import settings
CONTENT_TYPE_MAP = {
'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'png': 'image/png',
'webp': 'image/webp', 'gif': 'image/gif', 'bmp': 'image/bmp',
'tiff': 'image/tiff',
'mp4': 'video/mp4', 'mov': 'video/quicktime',
'mp3': 'audio/mpeg', 'wav': 'audio/wav',
}
_client = None
def get_tos_client():
import tos
global _client
if _client is None:
endpoint = settings.TOS_ENDPOINT.replace('https://', '').replace('http://', '')
_client = tos.TosClientV2(
ak=settings.TOS_ACCESS_KEY,
sk=settings.TOS_SECRET_KEY,
endpoint=endpoint,
region=settings.TOS_REGION,
)
return _client
def upload_file(file_obj, folder='uploads'):
"""Upload a file to TOS bucket, return its public URL."""
ext = file_obj.name.rsplit('.', 1)[-1].lower()
key = f'{folder}/{uuid.uuid4().hex}.{ext}'
content_type = CONTENT_TYPE_MAP.get(ext, 'application/octet-stream')
client = get_tos_client()
content = file_obj.read()
client.put_object(
bucket=settings.TOS_BUCKET,
key=key,
content=content,
content_type=content_type,
)
return f'{settings.TOS_CDN_DOMAIN}/{key}'
def upload_from_url(source_url, folder='results'):
"""Download a file from a URL and upload to TOS, return permanent CDN URL."""
import requests as req
resp = req.get(source_url, timeout=120, stream=True)
resp.raise_for_status()
content = resp.content
content_type = resp.headers.get('Content-Type', 'video/mp4')
ext = 'mp4' # Seedance always returns mp4
key = f'{folder}/{uuid.uuid4().hex}.{ext}'
client = get_tos_client()
client.put_object(
bucket=settings.TOS_BUCKET,
key=key,
content=content,
content_type=content_type,
)
return f'{settings.TOS_CDN_DOMAIN}/{key}'