Skip to content

Commit

Permalink
Add django storages to for manage the cloudflare R2 bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
stariluz committed Jun 23, 2024
1 parent 8000dfa commit 56cc866
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 10 deletions.
2 changes: 1 addition & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myFacegram.settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myFacegram.settings_production')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand Down
45 changes: 37 additions & 8 deletions myFacegram/settings_production.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@
# TODO Add host from back4app
ALLOWED_HOSTS = [
'127.0.0.1',
'localhost',
]


# Application definition

INSTALLED_APPS = [
# daphne
"daphne",
"storages",

# django
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
Expand All @@ -46,9 +52,6 @@
'posts',
'users',

# daphne

"daphne",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -143,10 +146,6 @@
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

MEDIA_ROOT = BASE_DIR / 'media'

MEDIA_URL = '/media/'

LOGIN_URL = '/users/login/'

CSRF_COOKIE_SECURE=True
Expand All @@ -155,4 +154,34 @@
# TODO Configure Cache

# Daphne
ASGI_APPLICATION = "myFacegram.asgi.application"
ASGI_APPLICATION = "myFacegram.asgi.application"

# Storage
# STORAGES = {
# "default": {
# "BACKEND": "storages.backends.s3.S3Storage",
# "OPTIONS": {
# "bucket_name": os.environ['AWS_STORAGE_BUCKET_NAME'],
# "endpoint_url": f"https://{os.environ['AWS_ACCOUNT_ID']}.r2.cloudflarestorage.com",
# "access_key": os.environ["AWS_ACCESS_KEY_ID"],
# "secret_key": os.environ["AWS_SECRET_ACCESS_KEY"],
# "default_acl": "private",
# "signature_version": "s3v4",
# },
# },
# "staticfiles": {
# "BACKEND": "storages.backends.s3.S3Storage",
# },
# }

AWS_ACCOUNT_ID=os.environ['AWS_ACCOUNT_ID']
DEFAULT_FILE_STORAGE = "storages.backends.s3.S3Storage"
AWS_S3_ENDPOINT_URL = f'https://{AWS_ACCOUNT_ID}.r2.cloudflarestorage.com'
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']
AWS_DEFAULT_ACL = 'private'
AWS_S3_SIGNATURE_VERSION='s3v4'

# MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# MEDIA_URL = f'{AWS_S3_ENDPOINT_URL}/{AWS_STORAGE_BUCKET_NAME}/'
123 changes: 122 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pillow = "^10.3.0"
pytz = "^2024.1"
sqlparse = "^0.5.0"
daphne = "^4.1.2"
django-storages = {extras = ["s3"], version = "^1.14.3"}


[build-system]
Expand Down
23 changes: 23 additions & 0 deletions tests/test-storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import boto3
from botocore.exceptions import ClientError
import os
print(os.environ.get('ACCESS_KEY_ID'))
print(os.environ.get('SECRET_ACCESS_KEY'))
# Environment variables
access_key_id = os.environ.get('ACCESS_KEY_ID')
secret_access_key = os.environ.get('SECRET_ACCESS_KEY')
endpoint_url = 'https://5868f1c65dbb2a8c20cd09e90d5d3c29.r2.cloudflarestorage.com'
bucket_name = 'my-facegram-storage'

s3 = boto3.client(
's3',
endpoint_url=endpoint_url,
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key
)

try:
s3.upload_file('testfile.txt', bucket_name, 'testfile.txt')
print("Upload Successful")
except ClientError as e:
print(f"Upload failed: {e}")
1 change: 1 addition & 0 deletions tests/testfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a testfile hehehe

0 comments on commit 56cc866

Please sign in to comment.