Spaces:
Sleeping
Sleeping
File size: 2,152 Bytes
54fe70d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
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") |