Skip to content

Commit 7cb0cc8

Browse files
committed
feat: integrate Cloudflare R2 for file storage
- Add R2 storage configuration in Django settings - Add .env.example template for R2 credentials - Update storage backend to use S3Boto3Storage - Add required packages: django-storages and boto3
1 parent 693ffdb commit 7cb0cc8

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ whitenoise==6.9.0
1111
djangorestframework==3.15.2
1212
django-cors-headers==4.6.0
1313
django-extensions==3.2.3
14+
15+
django-storages==1.14.5
16+
boto3==1.37.11

server/.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Cloudflare R2 Configuration
2+
R2_ACCESS_KEY_ID=your_access_key_id
3+
R2_SECRET_ACCESS_KEY=your_secret_access_key
4+
R2_ACCOUNT_ID=your_account_id
5+
R2_BUCKET_NAME=your_bucket_name

server/config/settings.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,22 @@ def create_log_dir_for_server_errors():
298298
INTERNAL_IPS = ['127.0.0.1']
299299

300300

301-
# https://whitenoise.readthedocs.io/en/stable/django.html
301+
# Storage configuration for Cloudflare R2
302+
AWS_ACCESS_KEY_ID = os.getenv('R2_ACCESS_KEY_ID')
303+
AWS_SECRET_ACCESS_KEY = os.getenv('R2_SECRET_ACCESS_KEY')
304+
AWS_S3_ENDPOINT_URL = f"https://{os.getenv('R2_ACCOUNT_ID')}.r2.cloudflarestorage.com"
305+
AWS_STORAGE_BUCKET_NAME = os.getenv('R2_BUCKET_NAME')
306+
AWS_S3_REGION_NAME = 'auto' # R2 doesn't need a specific region
307+
AWS_DEFAULT_ACL = 'public-read'
308+
AWS_QUERYSTRING_AUTH = False # Don't add complex authentication-related query parameters to URLs
309+
302310
STORAGES = {
303-
# ...
311+
# https://whitenoise.readthedocs.io/en/stable/django.html
304312
'staticfiles': {
305313
'BACKEND': 'whitenoise.storage.CompressedManifestStaticFilesStorage',
306314
},
307315
'default': {
308-
'BACKEND': 'django.core.files.storage.FileSystemStorage',
316+
'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
309317
},
310318
}
311319

@@ -318,5 +326,7 @@ def create_log_dir_for_server_errors():
318326
STATIC_ROOT = SERVER_PATH / 'static'
319327
STATIC_URL = '/static/'
320328

329+
# Media files configuration
330+
MEDIA_URL = f'https://{os.getenv("R2_BUCKET_NAME")}.r2.cloudflarestorage.com/'
331+
321332
MEDIA_ROOT = SERVER_PATH / 'uploads'
322-
MEDIA_URL = '/uploads/'

0 commit comments

Comments
 (0)