test_oss_upload.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. """
  3. OSS 上传测试脚本
  4. 测试本地图片上传到 MinIO
  5. """
  6. import os
  7. import sys
  8. import cv2
  9. import tempfile
  10. import logging
  11. import numpy as np
  12. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  13. logger = logging.getLogger(__name__)
  14. # 添加项目路径
  15. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  16. from dual_camera_system.oss_uploader import OSSUploader
  17. def create_test_image(width=640, height=480, text="Test Image"):
  18. """创建测试图片"""
  19. # 创建白色背景
  20. img = np.ones((height, width, 3), dtype=np.uint8) * 255
  21. # 画几个矩形
  22. cv2.rectangle(img, (50, 50), (width-50, height-50), (200, 200, 200), 2)
  23. cv2.rectangle(img, (100, 100), (width-100, height-100), (150, 150, 150), -1)
  24. # 添加文字
  25. cv2.putText(img, text, (width//4, height//2),
  26. cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
  27. return img
  28. def main():
  29. # 创建临时测试图片
  30. with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as tmp:
  31. temp_path = tmp.name
  32. try:
  33. # 生成测试图片
  34. img = create_test_image(640, 480, "OSS Test")
  35. cv2.imwrite(temp_path, img)
  36. logger.info(f"测试图片已创建: {temp_path}")
  37. # 初始化 OSS 上传器
  38. logger.info("初始化 OSS 上传器...")
  39. uploader = OSSUploader()
  40. if not uploader.enabled:
  41. logger.error("OSS 上传器未启用,请检查配置")
  42. return 1
  43. # 启动上传器
  44. logger.info("启动上传器...")
  45. uploader.start()
  46. # 测试上传
  47. test_key = "test/test_image.jpg"
  48. batch_id = "test_batch_001"
  49. logger.info(f"开始上传测试图片: {temp_path}")
  50. logger.info(f"OSS Key: {test_key}")
  51. # 同步上传测试
  52. result = uploader.upload_image_sync(
  53. local_path=temp_path,
  54. oss_key=test_key,
  55. batch_id=batch_id,
  56. image_type='test'
  57. )
  58. logger.info(f"上传结果: success={result.success}")
  59. logger.info(f"OSS URL: {result.oss_url}")
  60. if result.error:
  61. logger.error(f"错误信息: {result.error}")
  62. if result.success:
  63. logger.info("✅ OSS 上传测试成功!")
  64. else:
  65. logger.error("❌ OSS 上传测试失败!")
  66. return 1
  67. except Exception as e:
  68. logger.exception(f"测试异常: {e}")
  69. return 1
  70. finally:
  71. # 清理临时文件
  72. if os.path.exists(temp_path):
  73. os.remove(temp_path)
  74. logger.info(f"临时文件已清理: {temp_path}")
  75. return 0
  76. if __name__ == '__main__':
  77. sys.exit(main())