import os import requests import mimetypes def upload_to_oss(file_path, custom_filename=None): """ Uploads a file to OSS using the provided API endpoint. URL: https://crmapi.dcjxb.yunzhixue.cn/file/upload Method: POST Form-data: parameter 'file' """ url = "https://crmapi.dcjxb.yunzhixue.cn/file/upload" try: filename = custom_filename if custom_filename else os.path.basename(file_path) mime_type, _ = mimetypes.guess_type(file_path) if not mime_type: mime_type = 'application/octet-stream' with open(file_path, 'rb') as f: files = {'file': (filename, f, mime_type)} response = requests.post(url, files=files) response.raise_for_status() result = response.json() # Assuming the response contains the URL in a specific field if result.get('code') == 200 or result.get('success'): # Extract URL - adjusting based on common API patterns data = result.get('data', {}) if isinstance(data, str): return data return data.get('url') or data.get('fileUrl') or result.get('url') else: print(f"Upload failed: {result}") return None except Exception as e: print(f"Error uploading to OSS: {e}") return None