Spaces:
Sleeping
Sleeping
| import os | |
| import cloudinary | |
| import cloudinary.uploader | |
| from dotenv import load_dotenv | |
| import logging | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| # Load environment variables | |
| load_dotenv() | |
| # Configure Cloudinary | |
| cloudinary.config( | |
| cloud_name=os.getenv("CLOUDINARY_CLOUD_NAME"), | |
| api_key=os.getenv("CLOUDINARY_API_KEY"), | |
| api_secret=os.getenv("CLOUDINARY_API_SECRET"), | |
| secure=True | |
| ) | |
| def test_cloudinary_config(): | |
| print("Cloudinary configuration:") | |
| print(f"Cloud name: {cloudinary.config().cloud_name}") | |
| print(f"API key: {cloudinary.config().api_key}") | |
| print(f"API secret: {'*' * len(cloudinary.config().api_secret) if cloudinary.config().api_secret else 'Not set'}") | |
| def upload_test_image(): | |
| try: | |
| # Create a test file | |
| test_file_path = "cloudinary_test.txt" | |
| with open(test_file_path, "w") as f: | |
| f.write("Test content for Cloudinary upload") | |
| # Upload to Cloudinary | |
| print("Uploading test file to Cloudinary...") | |
| upload_result = cloudinary.uploader.upload( | |
| test_file_path, | |
| folder="marine_guard_test", | |
| resource_type="auto" # Let Cloudinary auto-detect the resource type | |
| ) | |
| print(f"Upload successful! URL: {upload_result.get('secure_url')}") | |
| print(f"Public ID: {upload_result.get('public_id')}") | |
| print(f"Resource type: {upload_result.get('resource_type')}") | |
| # Clean up the test file | |
| os.unlink(test_file_path) | |
| print(f"Deleted local test file: {test_file_path}") | |
| return upload_result | |
| except Exception as e: | |
| logger.error(f"Error uploading to Cloudinary: {e}", exc_info=True) | |
| if os.path.exists(test_file_path): | |
| os.unlink(test_file_path) | |
| return None | |
| if __name__ == "__main__": | |
| print("Testing detailed Cloudinary configuration and upload") | |
| test_cloudinary_config() | |
| result = upload_test_image() | |
| if result: | |
| print("✅ Cloudinary upload test PASSED") | |
| else: | |
| print("❌ Cloudinary upload test FAILED") |