test_oss_upload.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """测试 OSS 上传 + CDN 可访问性"""
  2. import asyncio
  3. import json
  4. import os
  5. import tempfile
  6. import httpx
  7. async def test():
  8. from cyber_sdk.ali_oss import upload_localfile
  9. # 创建测试图片 (1x1 PNG)
  10. tmp = os.path.join(tempfile.gettempdir(), "toolhub_test.png")
  11. with open(tmp, "wb") as f:
  12. f.write(
  13. b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01"
  14. b"\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89"
  15. b"\x00\x00\x00\nIDATx\x9cc\x00\x01\x00\x00\x05\x00\x01"
  16. b"\r\n-\xb4\x00\x00\x00\x00IEND\xaeB`\x82"
  17. )
  18. safe_path = os.path.abspath(tmp).replace("\\", "/")
  19. print(f"Uploading {safe_path} ...")
  20. result = await upload_localfile(
  21. file_path=safe_path,
  22. bucket_path="toolhub_images",
  23. bucket_name="aigc-admin",
  24. )
  25. print(f"Upload result: {json.dumps(result, ensure_ascii=False)}")
  26. oss_key = result.get("oss_object_key")
  27. if oss_key:
  28. cdn_url = f"https://res.cybertogether.net/{oss_key}"
  29. print(f"CDN URL: {cdn_url}")
  30. async with httpx.AsyncClient(timeout=10) as client:
  31. resp = await client.head(cdn_url)
  32. print(f"CDN HEAD status: {resp.status_code}")
  33. print(f"Content-Type: {resp.headers.get('content-type')}")
  34. print(f"Content-Length: {resp.headers.get('content-length')}")
  35. # 测试 Qwen 是否能访问(模拟:从不同网络 GET)
  36. async with httpx.AsyncClient(timeout=10) as client:
  37. resp = await client.get(cdn_url)
  38. print(f"CDN GET status: {resp.status_code}, size: {len(resp.content)} bytes")
  39. print("[PASS]" if resp.status_code == 200 else "[FAIL]")
  40. else:
  41. print("Upload failed: no oss_object_key")
  42. print("[FAIL]")
  43. os.unlink(tmp)
  44. if __name__ == "__main__":
  45. asyncio.run(test())