Cloudinary Python SDK

1.44.1 · active · verified Sat Apr 11

The Cloudinary Python SDK provides a comprehensive platform for managing digital media, including image and video upload, transformation, optimization, and delivery capabilities. It supports general Python applications as well as the Django framework. Currently at version 1.44.1, the library is actively maintained with frequent updates, typically focusing on new feature additions, API enhancements, and security improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure the Cloudinary Python SDK, upload an image from a remote URL, generate a transformed URL, and retrieve asset details using the Admin API. It emphasizes using environment variables (especially `CLOUDINARY_URL`) for secure configuration. For local development, `python-dotenv` is used to load these variables from a `.env` file.

import os
from dotenv import load_dotenv
import cloudinary
import cloudinary.uploader
import cloudinary.api

# Load environment variables from .env file (for local development)
load_dotenv()

# Configure Cloudinary using the CLOUDINARY_URL environment variable
# Example: CLOUDINARY_URL=cloudinary://<api_key>:<api_secret>@<cloud_name>
# Ensure this variable is set in your environment or .env file.
cloudinary.config(secure=True) # Forces HTTPS URLs

# Check if configuration is successful (optional, for debugging)
if not cloudinary.config().cloud_name:
    print("Cloudinary configuration missing. Please set CLOUDINARY_URL.")
    exit(1)

print("Cloudinary configured. Cloud name:", cloudinary.config().cloud_name)

# --- Example: Upload an image ---
# Replace 'my_local_image.jpg' with a path to a local image or a remote URL.
# For a real application, handle file paths dynamically.
try:
    upload_result = cloudinary.uploader.upload(
        "https://cloudinary-devs.github.io/cld-docs-assets/assets/images/butterfly.jpeg",
        public_id="quickstart_butterfly_example",
        unique_filename=False,
        overwrite=True
    )
    print("\nUpload successful:")
    print("Public ID:", upload_result['public_id'])
    print("Secure URL:", upload_result['secure_url'])

    # --- Example: Generate a transformed URL ---
    transformed_url = cloudinary.utils.cloudinary_url(
        upload_result['public_id'],
        width=100,
        height=150,
        crop="fill"
    )[0]
    print("Transformed URL (fill 100x150):", transformed_url)

    # --- Example: Get asset details (Admin API) ---
    asset_details = cloudinary.api.resource(upload_result['public_id'])
    print("\nAsset details:")
    print(f"Format: {asset_details['format']}, Bytes: {asset_details['bytes']}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →