基于火山引擎对象存储(TOS)的文件上传微服务,专为Dify工作流设计,支持音视频文件压缩和私有访问链接生成。
- 🚀 基于FastAPI的高性能异步服务
- 📦 支持文件上传到火山引擎对象存储
- 🎵 自动音频文件无损压缩
- 🎬 自动视频文件无损压缩
- 🔐 私有存储桶访问控制
- 🔗 签名URL访问机制
- 🔄 专为Dify工作流优化的API
- Python 3.9+
- FFmpeg
- Docker(可选)
- 克隆仓库
git clone https://github.com/Ivor-NCUT/tos-upload-service-for-Dify.git
cd tos-upload-service-for-Dify- 安装依赖
pip install -r requirements.txt- 配置环境变量
cp .env.example .env
# 编辑 .env 文件,填入必要的配置信息- 启动服务
uvicorn app.main:app --reloaddocker build -t tos-service .
docker run -d -p 8000:8000 --env-file .env tos-servicePOST /api/v1/upload
Content-Type: application/json
{
"file_name": "example.mp4",
"file_content": "base64_encoded_content",
"expires": 24 // 可选,URL有效期(小时)
}POST /api/v1/get_url
Content-Type: application/json
{
"object_key": "20250107_122916_example.mp4",
"expires": 24 // 可选,URL有效期(小时)
}POST /api/v1/delete
Content-Type: application/json
{
"object_key": "20250107_122916_example.mp4"
}import requests
import base64
import os
def upload_file(file_path, api_url):
# 读取文件并转换为Base64
with open(file_path, "rb") as f:
file_content = base64.b64encode(f.read()).decode()
# 准备请求数据
data = {
"file_name": os.path.basename(file_path),
"file_content": file_content,
"expires": 24
}
# 发送请求
response = requests.post(f"{api_url}/api/v1/upload", json=data)
return response.json()
# 使用示例
result = upload_file("example.mp4", "http://your-service-url:8000")
print(result["url"]) # 获取文件访问链接在 .env 文件中配置以下参数:
# TOS配置
TOS_ACCESS_KEY=your_access_key
TOS_SECRET_KEY=your_secret_key
TOS_ENDPOINT=your_endpoint
TOS_REGION=your_region
BUCKET_NAME=your_bucket_name
# URL配置
URL_EXPIRE_HOURS=24- MP3 (.mp3)
- WAV (.wav)
- AAC (.aac)
- FLAC (.flac)
- MP4 (.mp4)
- AVI (.avi)
- MKV (.mkv)
- MOV (.mov)
- 文件大小限制:建议单个文件不超过1GB
- URL有效期:默认24小时,可在请求时指定
- 安全性:建议在生产环境中使用HTTPS
- 临时文件:服务会自动清理处理过程中的临时文件
- Ivor-NCUT
- Email: 754746843@qq.com
欢迎提交问题和功能请求!如果您想贡献代码,请:
- Fork 本仓库
- 创建您的特性分支 (git checkout -b feature/AmazingFeature)
- 提交您的更改 (git commit -m 'Add some AmazingFeature')
- 推送到分支 (git push origin feature/AmazingFeature)
- 打开一个 Pull Request