django-storages
django-storages copied to clipboard
How do I use R2 instead of S3?
I read the blog: Using Cloudflare R2 with Django Storages (https://medium.com/@asanka_l/using-cloudflare-r2-with-django-storages-40ebeabad79f)
But I want to choose R2 in public with django-storages
Thank you
@re4lfl0w I just set this up and while that blog post was helpful, I had to adjust a few things.
The blog post uses an older format for settings. In Django versions >= 4.2, there is only one top-level variable called STORAGES
, which contains a dictionary. In my case it looks like this:
STATIC_URL = env('STATIC_URL')
STORAGES = {
"staticfiles": {
"BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
'bucket_name': env('AWS_STORAGE_BUCKET_NAME'),
'endpoint_url': env('AWS_S3_ENDPOINT_URL'),
},
},
}
The env()
is from django-environ. My .env
file looks like this:
STATIC_URL=https://static.example.com/
AWS_ACCESS_KEY_ID=your-access-key-id
AWS_SECRET_ACCESS_KEY=your-secret-access-key
AWS_STORAGE_BUCKET_NAME=your-bucket
AWS_S3_ENDPOINT_URL=https://<ACCOUNT_ID>.r2.cloudflarestorage.com
On the Cloudflare dashboard I’ve set static.example.com
as custom domain on the R2 bucket.
I hope that helps!