Ai_GirlFriend/test_oss_upload.py
2026-03-04 12:04:21 +08:00

62 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
测试 OSS 上传功能
"""
import sys
import os
sys.path.append('.')
from lover.oss_utils import test_oss_connection, upload_audio_file, delete_audio_file
import logging
# 设置日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def test_oss_upload():
"""测试 OSS 上传功能"""
# 1. 测试连接
logger.info("=== 测试 OSS 连接 ===")
if not test_oss_connection():
logger.error("OSS 连接失败,无法继续测试")
return False
# 2. 创建测试音频数据
logger.info("=== 创建测试音频数据 ===")
test_audio_data = b"fake audio data for testing" * 1000 # 创建一些测试数据
logger.info(f"测试数据大小: {len(test_audio_data)} 字节")
# 3. 上传测试
logger.info("=== 测试上传 ===")
try:
file_url = upload_audio_file(test_audio_data, "mp3")
logger.info(f"上传成功URL: {file_url}")
# 验证 URL 格式
if file_url.startswith('https://'):
logger.info("✅ URL 格式正确")
else:
logger.error(f"❌ URL 格式错误: {file_url}")
return False
# 4. 删除测试
logger.info("=== 测试删除 ===")
if delete_audio_file(file_url):
logger.info("✅ 删除成功")
else:
logger.warning("⚠️ 删除失败")
return True
except Exception as e:
logger.error(f"❌ 上传测试失败: {e}")
return False
if __name__ == "__main__":
success = test_oss_upload()
if success:
logger.info("🎉 OSS 上传测试通过")
else:
logger.error("💥 OSS 上传测试失败")
sys.exit(1)