Skip to main content

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-storages for 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

  1. Add DjangoCloudflareCDN to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    # other apps
    'DjangoCloudflareCDN',
]
  1. Import the Cloudflare R2 settings in your settings.py
# settings.py

from DjangoCloudflareCDN import cloudflare_r2
  1. 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'
  1. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_cloudflare_cdn-1.0.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_cloudflare_cdn-1.0.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

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

Hashes for django_cloudflare_cdn-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9656160cf576fb9b65f862fe99da2f4a8604ba4fb2fb085974bfa9530c58b2a1
MD5 43f7f9f124e07cc5e6d849699081e536
BLAKE2b-256 4fa50a3b113fb29c68b067eba6249f7e34cd7ae65181546bf0564960926264dc

See more details on using hashes here.

File details

Details for the file django_cloudflare_cdn-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_cloudflare_cdn-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 22e24628e559ad9cdbf59c48dbf6732ddfde6e3ed058ca729722635483104137
MD5 5e391db7de3edd61e16673315ae40a02
BLAKE2b-256 5b223ec8fddcd31224d14c646beb0a2629e1e18a010ef244dccdb14c64fb43b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page