upload_avatars.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """
  2. 上传预设数字人头像到OSS
  3. 运行: python -m scripts.upload_avatars
  4. """
  5. import os
  6. import sys
  7. from pathlib import Path
  8. sys.path.insert(0, str(Path(__file__).parent.parent))
  9. from dotenv import load_dotenv
  10. load_dotenv()
  11. from app.services.oss_service import get_oss_service
  12. AVATARS_DIR = Path(__file__).parent.parent.parent / "frontend" / "assets" / "avatars"
  13. AVATAR_FILES = [
  14. "职场精英-陆远.png",
  15. "科技达人-艾达.png",
  16. "亲和主持-佳莹.png",
  17. "青春活力-小星.png",
  18. ]
  19. def upload_avatars():
  20. oss_service = get_oss_service()
  21. results = {}
  22. for filename in AVATAR_FILES:
  23. filepath = AVATARS_DIR / filename
  24. if not filepath.exists():
  25. print(f"文件不存在: {filepath}")
  26. continue
  27. with open(filepath, "rb") as f:
  28. file_data = f.read()
  29. url = oss_service.upload_file(file_data, "preset-avatars", filename)
  30. results[filename] = url
  31. print(f"已上传: {filename} -> {url}")
  32. print("\n请将以下URL配置到前端代码中:")
  33. for filename, url in results.items():
  34. print(f' "{filename}": "{url}"')
  35. if __name__ == "__main__":
  36. upload_avatars()