A Django package for serving static files via S3-compatible CDN (Cloudflare R2).
Project description
DjangoCloudflareCDN
Overview
DjangoCloudflareCDN is a simple Python package that allows you to easily integrate Cloudflare R2 with Django for serving static and media files. It provides a seamless setup using django-storages and boto3 to configure Django’s storage backends for Cloudflare R2.
Features
- Integrates Cloudflare R2 as the static and media file storage for Django.
- Supports custom domain setup for Cloudflare R2.
- Automatically configures
django-storagesfor S3-compatible storage with Cloudflare R2. - Provides customizable settings for Cloudflare R2, including access keys and cache control.
Installation
pip install django-cloudflare-cdn
Usage
- Add DjangoCloudflareCDN to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
# other apps
'DjangoCloudflareCDN',
]
- Import the Cloudflare R2 settings in your settings.py
# settings.py
from DjangoCloudflareCDN import cloudflare_r2
- Configure Cloudflare R2 credentials and settings
# settings.py
# Cloudflare R2 Settings
AWS_ACCESS_KEY_ID = '<your-access-key>'
AWS_SECRET_ACCESS_KEY = '<your-secret-key>'
AWS_STORAGE_BUCKET_NAME = '<your-bucket-name>'
AWS_S3_ENDPOINT_URL = 'https://<your-account-id>.r2.cloudflarestorage.com'
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400'
}
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.r2.cloudflarestorage.com'
# Static File Settings
STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/'
# Use S3Boto3Storage as the default storage backend
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
- Run the collectstatic command to upload your static files to Cloudflare R2 (Depends on api configuration)
python manage.py collectstatic
Optional Settings
Custom ACL for Files: Uncomment the AWS_DEFAULT_ACL line to make all files publicly readable.
#settins.py
AWS_DEFAULT_ACL = 'public-read'
Cache Control: Customize the cache control settings for your files.
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400'
}
Troubleshooting
-
Ensure that your Cloudflare R2 bucket is properly configured.
-
Double-check your Cloudflare R2 credentials (Access Key and Secret Key).
-
If files are not showing up, ensure that you have run the collectstatic command.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_cloudflare_cdn-1.0.0.tar.gz.
File metadata
- Download URL: django_cloudflare_cdn-1.0.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9656160cf576fb9b65f862fe99da2f4a8604ba4fb2fb085974bfa9530c58b2a1
|
|
| MD5 |
43f7f9f124e07cc5e6d849699081e536
|
|
| BLAKE2b-256 |
4fa50a3b113fb29c68b067eba6249f7e34cd7ae65181546bf0564960926264dc
|
File details
Details for the file django_cloudflare_cdn-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_cloudflare_cdn-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22e24628e559ad9cdbf59c48dbf6732ddfde6e3ed058ca729722635483104137
|
|
| MD5 |
5e391db7de3edd61e16673315ae40a02
|
|
| BLAKE2b-256 |
5b223ec8fddcd31224d14c646beb0a2629e1e18a010ef244dccdb14c64fb43b6
|