oss_utils.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. import requests
  2. def upload_to_oss(file_path):
  3. """
  4. Uploads a file to OSS using the provided API endpoint.
  5. URL: https://crmapi.dcjxb.yunzhixue.cn/file/upload
  6. Method: POST
  7. Form-data: parameter 'file'
  8. """
  9. url = "https://crmapi.dcjxb.yunzhixue.cn/file/upload"
  10. try:
  11. with open(file_path, 'rb') as f:
  12. files = {'file': f}
  13. response = requests.post(url, files=files)
  14. response.raise_for_status()
  15. result = response.json()
  16. # Assuming the response contains the URL in a specific field
  17. # We'll need to check the actual response format
  18. # Typical format: {"code": 200, "data": {"url": "..."}} or similar
  19. if result.get('code') == 200 or result.get('success'):
  20. # Extract URL - adjusting based on common API patterns
  21. data = result.get('data', {})
  22. if isinstance(data, str):
  23. return data
  24. return data.get('url') or data.get('fileUrl') or result.get('url')
  25. else:
  26. print(f"Upload failed: {result}")
  27. return None
  28. except Exception as e:
  29. print(f"Error uploading to OSS: {e}")
  30. return None