Skip to content

Commit ca69197

Browse files
committed
Use django storages for uploading public files
1 parent 09c380b commit ca69197

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

config/django/base.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
'django_celery_results',
5050
'django_celery_beat',
5151
'django_filters',
52-
'corsheaders'
52+
'corsheaders',
53+
'storages'
5354
]
5455

5556
INSTALLED_APPS = [
@@ -170,13 +171,8 @@
170171

171172
SERVER_HOST_DOMAIN = env("SERVER_HOST_DOMAIN", default="http://localhost:8000")
172173

173-
# # Media
174-
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
175-
MEDIA_URL = "/media/"
176-
177-
178174
from config.settings.cors import * # noqa
179175
from config.settings.sessions import * # noqa
180176
from config.settings.celery import * # noqa
181177
from config.settings.sentry import * # noqa
182-
from config.settings.aws import * # noqa
178+
from config.settings.file_upload import * # noqa

config/settings/aws.py renamed to config/settings/file_upload.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1+
import os
12
from config.env import env
3+
from config.django.base import BASE_DIR
4+
5+
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
6+
MEDIA_URL = "/media/"
7+
8+
DEFAULT_FILE_STORAGE = "django.core.files.storage.FileSystemStorage"
29

3-
DEFAULT_FILE_STORAGE = env(
4-
"DEFAULT_FILE_STORAGE",
5-
default="django.core.files.storage.FileSystemStorage",
6-
)
710

811
USE_S3_UPLOAD = env("USE_S3_UPLOAD", default=False)
912

1013
if USE_S3_UPLOAD:
14+
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
15+
1116
AWS_ACCESS_KEY_ID = env("AWS_ACCESS_KEY_ID")
1217
AWS_SECRET_ACCESS_KEY = env("AWS_SECRET_ACCESS_KEY")
1318
AWS_STORAGE_BUCKET_NAME = env("AWS_STORAGE_BUCKET_NAME")
1419

1520
AWS_FILES_EXPIRY = 60 * 60 # 1 hour. Change this configuration if needed
21+
AWS_S3_FILE_OVERWRITE = False
22+
AWS_DEFAULT_ACL = env("AWS_DEFAULT_ACL", default='public-read')
1623

17-
AWS_S3_REGION_NAME = env("AWS_S3_REGION_NAME")
18-
AWS_S3_CUSTOM_DOMAIN = env("AWS_S3_CUSTOM_DOMAIN")
24+
# AWS_S3_REGION_NAME = env("AWS_S3_REGION_NAME")
25+
AWS_S3_CUSTOM_DOMAIN = env("AWS_S3_CUSTOM_DOMAIN", default=None)
1926
AWS_S3_DOMAIN = (
2027
AWS_S3_CUSTOM_DOMAIN or f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
2128
)

requirements/base.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ django-filter==21.1
1313
django-cors-headers==3.10.0
1414

1515
boto3==1.20.20
16+
17+
django-storages==1.12.3

styleguide_example/files/migrations/0001_initial.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# Generated by Django 3.2.9 on 2021-12-06 15:55
1+
# Generated by Django 3.2.9 on 2022-02-03 13:57
22

33
from django.conf import settings
44
from django.db import migrations, models
55
import django.db.models.deletion
66
import django.utils.timezone
7-
import styleguide_example.files.utils
87

98

109
class Migration(migrations.Migration):
@@ -22,7 +21,7 @@ class Migration(migrations.Migration):
2221
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
2322
('created_at', models.DateTimeField(db_index=True, default=django.utils.timezone.now)),
2423
('updated_at', models.DateTimeField(auto_now=True)),
25-
('file', models.FileField(blank=True, null=True, upload_to=styleguide_example.files.utils.file_generate_upload_path)),
24+
('file', models.FileField(blank=True, null=True, upload_to='files')),
2625
('file_name', models.CharField(max_length=255)),
2726
('file_type', models.CharField(max_length=255)),
2827
('uploaded_at', models.DateTimeField(blank=True, null=True)),

styleguide_example/files/models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
from styleguide_example.users.models import BaseUser
77

8-
from styleguide_example.files.utils import file_generate_upload_path
8+
# from styleguide_example.files.utils import file_generate_upload_path
99

1010

1111
class File(BaseModel):
12-
file = models.FileField(upload_to=file_generate_upload_path, null=True, blank=True)
12+
file = models.FileField(upload_to="files", null=True, blank=True) # TODO: DO we need this to be nullable?
1313
file_name = models.CharField(max_length=255)
1414
file_type = models.CharField(max_length=255)
1515

@@ -18,6 +18,9 @@ class File(BaseModel):
1818

1919
@property
2020
def url(self):
21+
if not bool(self.file): # TODO: DO we need this?
22+
return ''
23+
2124
if settings.USE_S3_UPLOAD:
2225
return self.file.url
2326

0 commit comments

Comments
 (0)