I am a long fan and user of Cloudflare and with Cloudflare R2 i am more in love with cloudflare.
Cloudflare R2 is one of the amazing and interesting service that Cloudflare has released. It's called,
Rapid and Reliable Object Storage, minus the egress fees and gives you the freedom to create the multi-cloud architectures you desire with an S3-compatible distributed object storage.
Which means, it's a object storage and the amazing part is egress charges is free.
You can try and play around R2 for free with its free Monthly Quota and trust me the free limit will be enough to build an app, up and running easily.
Grab secret keys and bucket info from Cloudflare
Creating a Bucket On Cloudflare Dashboard go to R2 and create a bucket
Note: As of now, Buckets can only be private.
Create API tokens to communicate with R2 Go to
Manage R2 API Tokens
from R2 Dashboard and create a API token which consist ofaccess_key
andsecret_key
Make sure you copy these to safe location
- Account ID : You can get it from the URL section, or from the bucket url or from the right most div on the R2 dashboard
- Bucket name: You just created
- Access key: You just obtained while creating API tokens
- Secret Key: You just obtained while creating API tokens
Implementing on Django
Since, R2 is compatible with S3 apis* we can easily use third party packages that are using S3 api for object storage.
Let's install the required libraries
pip install django-storages
pip install boto3
on Django settings.py
add the following,
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
AWS_STORAGE_BUCKET_NAME = '<YOUR_BUCKET_NAME>'
AWS_S3_ENDPOINT_URL = 'https://<YOUR_ACCOUNT_ID>.r2.cloudflarestorage.com'
AWS_S3_ACCESS_KEY_ID = '<YOUR_ACCESS_KEY>'
AWS_S3_SECRET_ACCESS_KEY = '<YOUR_SECRET_KEY>'
AWS_S3_SIGNATURE_VERSION = 's3v4'
Make sure: signature version is
s3v4
since Cloudflare R2 doesn't support SigV2 and we need to explicitly set it to SigV4
That's it, you can use Django freely with R2 now.
General Tips/Helps
- Since, R2 currently supports Private bucket only, every object should be pre signed. Using
django-storages
with the above configurations does that to us automatically. In simple word it meansgenerating a presigned url for object
- Since, a bucket cannot be made public as of now, i wouldn't suggest storing static files from Django on R2
- Since Cloudflare R2 api is compatible with S3 api, we are able to use
boto3
and S3 API to communicate with R2 - Public Buckets are currently in roadmap but not implemented. (I am really waiting and excited for this)
- Learn more about R2 from Cloudflare docs at https://developers.cloudflare.com/r2/
Tested on
- Django==4.0.6
- django-storages==1.12.3
- boto3==1.24.28
Last Updated: July 13, 2022