-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudinary_provider.py
More file actions
354 lines (296 loc) · 12.1 KB
/
cloudinary_provider.py
File metadata and controls
354 lines (296 loc) · 12.1 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
"""
Cloudinary Provider for Image Upload Utility
This module provides Cloudinary integration as an alternative to AWS CloudFront
for image uploading, optimization, and delivery.
Features:
- Automatic image optimization (format, quality, dimensions)
- Built-in CDN delivery
- Dynamic URL-based transformations
- Responsive image support
- Direct upload from URLs or local files
Usage:
from cloudinary_provider import CloudinaryProvider
provider = CloudinaryProvider()
result = provider.upload_image(file_path, optimization_options)
"""
import os
import time
import urllib.parse
from typing import Any, Dict, Optional, Tuple
try:
import cloudinary
import cloudinary.api
import cloudinary.uploader
import cloudinary.utils
from cloudinary.exceptions import Error as CloudinaryError
CLOUDINARY_AVAILABLE = True
except ImportError:
CLOUDINARY_AVAILABLE = False
class CloudinaryProvider:
"""Cloudinary image upload and optimization provider"""
def __init__(self):
"""Initialize Cloudinary provider with configuration from environment variables"""
if not CLOUDINARY_AVAILABLE:
raise ImportError(
"Cloudinary package not available. Install with: pip install cloudinary"
)
# Get configuration from environment variables
self.cloud_name = os.getenv("CLOUDINARY_CLOUD_NAME")
self.api_key = os.getenv("CLOUDINARY_API_KEY")
self.api_secret = os.getenv("CLOUDINARY_API_SECRET")
if not all([self.cloud_name, self.api_key, self.api_secret]):
raise ValueError(
"Cloudinary configuration incomplete. Please set CLOUDINARY_CLOUD_NAME, "
"CLOUDINARY_API_KEY, and CLOUDINARY_API_SECRET environment variables."
)
# Configure Cloudinary
cloudinary.config(
cloud_name=self.cloud_name,
api_key=self.api_key,
api_secret=self.api_secret,
secure=True,
)
print(f"✅ Cloudinary configured for cloud: {self.cloud_name}")
def test_connection(self) -> bool:
"""Test Cloudinary API connection"""
try:
# Test by getting account details
result = cloudinary.api.ping()
print(f"✅ Cloudinary connection successful: {result}")
return True
except Exception as e:
print(f"❌ Cloudinary connection failed: {e}")
return False
def upload_image(
self,
file_path: str,
file_name: str,
max_width: Optional[int] = None,
quality: int = 82,
smart_format: bool = True,
add_timestamp: bool = True,
folder: str = "images",
) -> Tuple[bool, Optional[str], Optional[Dict[str, Any]]]:
"""
Upload an image to Cloudinary with optimization
Args:
file_path: Path to the local image file
file_name: Original filename
max_width: Maximum width for resizing (None = no resizing)
quality: Image quality (1-100, 'auto' for automatic)
smart_format: Enable automatic format selection
add_timestamp: Add timestamp to filename for uniqueness
folder: Cloudinary folder for organization
Returns:
Tuple of (success, cloudinary_url, upload_result)
"""
try:
# Prepare filename
base_name, ext = os.path.splitext(file_name.lower())
if add_timestamp:
timestamp = int(time.time())
public_id = f"{base_name}_{timestamp}"
else:
public_id = base_name
# Build transformation parameters
transformation_params = {}
# Add width constraint if specified
if max_width:
transformation_params["width"] = max_width
transformation_params["crop"] = "scale"
# Add quality and format settings
if smart_format:
transformation_params["format"] = "auto"
transformation_params["quality"] = "auto"
else:
transformation_params["quality"] = quality
# Add automatic optimization
transformation_params["gravity"] = "auto" # Smart cropping if needed
# Upload options
upload_options = {
"public_id": public_id,
"folder": folder,
"resource_type": "image",
"invalidate": True, # Invalidate CDN cache
"overwrite": False, # Don't overwrite existing files
}
# Add transformation if any parameters were set
if transformation_params:
upload_options["transformation"] = transformation_params
print(f"📤 Uploading {file_name} to Cloudinary...")
print(f" Public ID: {folder}/{public_id}")
print(
f" Transformations: {transformation_params if transformation_params else 'None'}"
)
# Upload the file
result = cloudinary.uploader.upload(file_path, **upload_options)
# Generate optimized URL
cloudinary_url = result.get("secure_url")
if cloudinary_url:
print(f"✅ Successfully uploaded to Cloudinary: {cloudinary_url}")
return True, cloudinary_url, result
else:
print("❌ Upload succeeded but no URL returned")
return False, None, result
except CloudinaryError as e:
print(f"❌ Cloudinary upload error: {e}")
return False, None, None
except Exception as e:
print(f"❌ Unexpected error during upload: {e}")
return False, None, None
def upload_from_url(
self,
source_url: str,
max_width: Optional[int] = None,
quality: int = 82,
smart_format: bool = True,
add_timestamp: bool = True,
folder: str = "images",
) -> Tuple[bool, Optional[str], Optional[Dict[str, Any]]]:
"""
Upload an image directly from URL to Cloudinary
Args:
source_url: URL of the image to upload
max_width: Maximum width for resizing
quality: Image quality (1-100)
smart_format: Enable automatic format selection
add_timestamp: Add timestamp to filename
folder: Cloudinary folder for organization
Returns:
Tuple of (success, cloudinary_url, upload_result)
"""
try:
# Extract filename from URL
parsed_url = urllib.parse.urlparse(source_url)
file_name = os.path.basename(parsed_url.path)
base_name, ext = os.path.splitext(file_name.lower())
if add_timestamp:
timestamp = int(time.time())
public_id = f"{base_name}_{timestamp}"
else:
public_id = base_name
# Build transformation parameters
transformation_params = {}
if max_width:
transformation_params["width"] = max_width
transformation_params["crop"] = "scale"
if smart_format:
transformation_params["format"] = "auto"
transformation_params["quality"] = "auto"
else:
transformation_params["quality"] = quality
transformation_params["gravity"] = "auto"
# Upload options
upload_options = {
"public_id": public_id,
"folder": folder,
"resource_type": "image",
"invalidate": True,
"overwrite": False,
}
if transformation_params:
upload_options["transformation"] = transformation_params
print(f"📤 Uploading from URL to Cloudinary: {source_url}")
print(f" Public ID: {folder}/{public_id}")
print(
f" Transformations: {transformation_params if transformation_params else 'None'}"
)
# Upload directly from URL
result = cloudinary.uploader.upload(source_url, **upload_options)
cloudinary_url = result.get("secure_url")
if cloudinary_url:
print(f"✅ Successfully uploaded from URL: {cloudinary_url}")
return True, cloudinary_url, result
else:
print("❌ Upload succeeded but no URL returned")
return False, None, result
except CloudinaryError as e:
print(f"❌ Cloudinary upload error: {e}")
return False, None, None
except Exception as e:
print(f"❌ Unexpected error during URL upload: {e}")
return False, None, None
def generate_responsive_url(
self,
public_id: str,
folder: str = "images",
transformations: Optional[Dict[str, Any]] = None,
) -> str:
"""
Generate a responsive Cloudinary URL with dynamic transformations
Args:
public_id: The Cloudinary public ID
folder: Cloudinary folder
transformations: Additional transformation parameters
Returns:
Responsive Cloudinary URL
"""
try:
# Base transformations for responsive images
base_transformations = {
"format": "auto",
"quality": "auto",
"gravity": "auto",
"crop": "scale",
}
# Merge with provided transformations
if transformations:
base_transformations.update(transformations)
# Generate URL
url = cloudinary.utils.cloudinary_url(
f"{folder}/{public_id}", **base_transformations
)[0]
return url
except Exception as e:
print(f"❌ Error generating responsive URL: {e}")
return ""
def get_upload_stats(self) -> Dict[str, Any]:
"""Get account usage statistics"""
try:
usage = cloudinary.api.usage()
return {
"credits_used": usage.get("credits", {}).get("used", 0),
"credits_limit": usage.get("credits", {}).get("limit", 0),
"transformations": usage.get("transformations", {}).get("used", 0),
"storage": usage.get("storage", {}).get("used", 0),
"bandwidth": usage.get("bandwidth", {}).get("used", 0),
}
except Exception as e:
print(f"❌ Error getting upload stats: {e}")
return {}
def delete_image(self, public_id: str, folder: str = "images") -> bool:
"""Delete an image from Cloudinary"""
try:
result = cloudinary.uploader.destroy(f"{folder}/{public_id}")
return result.get("result") == "ok"
except Exception as e:
print(f"❌ Error deleting image: {e}")
return False
def test_cloudinary_connection() -> bool:
"""Test function for Cloudinary connection"""
try:
provider = CloudinaryProvider()
return provider.test_connection()
except Exception as e:
print(f"❌ Cloudinary test failed: {e}")
return False
if __name__ == "__main__":
# Test the Cloudinary provider
print("🧪 Testing Cloudinary Provider")
print("=" * 40)
if test_cloudinary_connection():
provider = CloudinaryProvider()
stats = provider.get_upload_stats()
if stats:
print("📊 Account Stats:")
print(f" Credits Used: {stats.get('credits_used', 'N/A')}")
print(f" Storage Used: {stats.get('storage', 'N/A')} bytes")
print(f" Transformations: {stats.get('transformations', 'N/A')}")
else:
print("✅ Cloudinary integration is working correctly!")
print("\n🚀 Ready to use:")
print(" ./process_csv.sh (choose Cloudinary option)")
print(" python unified_upload.py --provider cloudinary --mode csv")
else:
print("❌ Cloudinary connection test failed")