Spaces:
Sleeping
Sleeping
File size: 4,359 Bytes
16d5a75 744b763 16d5a75 744b763 16d5a75 |
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 |
import os
import cloudinary
import cloudinary.uploader
import cloudinary.api
from cloudinary.utils import cloudinary_url
from typing import Dict, Any, Optional, Union, List, Tuple
import os
# Configuration
def configure_cloudinary() -> None:
"""Configure Cloudinary with credentials from environment variables or directly."""
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,
)
# Initialize configuration
configure_cloudinary()
def upload_image(file_path: str, **options) -> Dict[str, Any]:
"""Upload an image to Cloudinary and return the result.
Args:
file_path: Local path to the image file or URL of an image
**options: Additional options for the upload
- public_id: Custom public ID for the image
- folder: Folder in Cloudinary to store the image
- resource_type: Type of resource ('image', 'video', 'raw', etc.)
- tags: List of tags to assign to the image
- transformation: Transformation to apply during upload
- format: Format to convert the image to
- overwrite: Whether to overwrite existing images with the same public_id
Returns:
Dictionary containing upload result with keys like 'public_id', 'secure_url', etc.
"""
# Set default options if not provided
if "resource_type" not in options:
options["resource_type"] = "image"
# Upload the image
result = cloudinary.uploader.upload(file_path, **options)
return result
def delete_image(public_id: str, **options) -> Dict[str, Any]:
"""Delete an image from Cloudinary.
Args:
public_id: Public ID of the image to delete
**options: Additional options for deletion
- resource_type: Type of resource ('image', 'video', 'raw', etc.)
- invalidate: Whether to invalidate CDN cache
Returns:
Dictionary containing the deletion result
"""
# Set default options if not provided
if "resource_type" not in options:
options["resource_type"] = "image"
# Delete the image
result = cloudinary.uploader.destroy(public_id, **options)
return result
def update_image(public_id: str, file_path: str, **options) -> Dict[str, Any]:
"""Update an existing image by uploading a new one with the same public_id.
Args:
public_id: Public ID of the image to update
file_path: Local path to the new image file or URL of an image
**options: Additional options for the upload
Returns:
Dictionary containing upload result
"""
# Ensure we're overwriting the existing image
options["public_id"] = public_id
options["overwrite"] = True
# Upload the new image with the same public_id
result = upload_image(file_path, **options)
return result
def get_image_url(public_id: str, **options) -> str:
"""Generate a URL for an image with optional transformations.
Args:
public_id: Public ID of the image
**options: Transformation options
- width: Width to resize to
- height: Height to resize to
- crop: Cropping method ('fill', 'fit', 'limit', 'thumb', 'crop', etc.)
- fetch_format: Format to deliver the image in ('auto', 'jpg', 'png', etc.)
- quality: Quality of the image ('auto', or 1-100)
- effect: Effects to apply
- gravity: How to position the image ('auto', 'face', 'center', etc.)
Returns:
URL string for the transformed image
"""
url, _ = cloudinary_url(public_id, **options)
return url
def list_images(prefix: Optional[str] = None, **options) -> Dict[str, Any]:
"""List images in your Cloudinary account.
Args:
prefix: Filter images by prefix
**options: Additional options for listing
- type: Resource type ('upload', 'private', etc.)
- max_results: Maximum number of results to return
- next_cursor: For pagination
- tags: Filter by tags
Returns:
Dictionary containing the list of images
"""
if prefix:
options["prefix"] = prefix
result = cloudinary.api.resources(**options)
return result
|