Django Cloudflare R2 Integration
A Django web application that integrates Cloudflare R2 as a cost-effective, S3-compatible storage backend for static and media files.

Description
This project demonstrates how to configure a Django application to seamlessly integrate with Cloudflare R2, a globally distributed and cost-effective object storage service. By leveraging R2’s S3-compatible API, the project enables Django developers to store and serve both static assets (CSS, JavaScript, images) and user-uploaded media files directly from Cloudflare’s infrastructure without incurring egress fees.
The integration is achieved through custom storage backends built on top of django-storages and boto3. These backends provide separate, dedicated configurations for static files and media uploads, ensuring that each type of content is handled efficiently and securely. The project also emphasizes environment-based configuration using python-decouple, allowing sensitive credentials such as access keys and bucket endpoints to be managed securely outside of the codebase.
Beyond basic setup, the project includes a production-ready configuration checklist covering security best practices, deployment considerations, and troubleshooting. Developers are guided through creating R2 buckets, generating API tokens with appropriate permissions, and configuring CORS and bucket policies for safe public access. The documentation also highlights how to use Django’s collectstatic command to upload static files to R2, ensuring smooth deployment workflows.
The repository provides practical examples of how to use R2-backed storage in Django models, including FileField and ImageField usage for documents and profile avatars. Uploaded files are automatically stored in the configured R2 bucket and served via Cloudflare’s global network, with optional support for custom domains and CDN integration. This makes the project suitable not only for development and testing but also for production-grade deployments.
Overall, this project serves as a reference implementation for developers who want to reduce infrastructure costs, improve scalability, and take advantage of Cloudflare’s global performance and security features while continuing to work within Django’s familiar storage framework.
Features
Cloudflare R2 Storage Integration
Configures Django to use Cloudflare R2 as the storage backend for static and media files.
Environment-Based Configuration
Manages sensitive credentials and settings via environment variables using python-decouple.
Custom Storage Classes
Provides dedicated classes for StaticFilesStorage and MediaFileStorage in Django.
S3-Compatible Interface
Ensures smooth integration with R2 using boto3, leveraging AWS S3-compatible APIs.
Production-Ready Setup
Includes deployment checklist, security best practices, and collectstatic integration.