Skip to content

Commit

Permalink
S3 Storage now save media in dedicated media folder with better naming
Browse files Browse the repository at this point in the history
Related to #44
  • Loading branch information
sebastienbarbier committed Oct 9, 2024
1 parent 4fe749d commit 395307c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ See for sample https://raw.githubusercontent.com/favoloso/conventional-changelog
## [0.10.0] - 2024-MM-DD
### 🐛 Bug Fixes
- Fix admin % lighthouse inQueue value (#42)
- Performance folder is not deleted with model (#44)
- Refactor media storage for scalability (#44)

## [0.9.2] - 2024-05-20
### Improvements
Expand Down
8 changes: 4 additions & 4 deletions django/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@
AWS_S3_CUSTOM_DOMAIN = os.getenv('AWS_S3_CUSTOM_DOMAIN')
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=3600'}
AWS_S3_FILE_OVERWRITE = True
STATIC_ROOT = os.path.join(BASE_DIR, 'collectstatic')
# s3 static settings
AWS_LOCATION = 'static'
STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{AWS_LOCATION}/'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATIC_ROOT = os.path.join(BASE_DIR, 'collectstatic')
STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/static/'
DEFAULT_FILE_STORAGE = 'core.storage_backends.MediaStorage'
MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/media/'

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
# Application definition
Expand Down
5 changes: 5 additions & 0 deletions django/core/storage_backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from storages.backends.s3boto3 import S3Boto3Storage

class MediaStorage(S3Boto3Storage):
location = 'media'
file_overwrite = False
6 changes: 3 additions & 3 deletions django/performances/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def __str__(self):
return f'{self.url}'

def directory_path(self):
user = self.project.user
slug = slugify(self.url)
return f'performance/{user}/{slug}'
user_pk = self.project.user.pk
project_pk = self.project.pk
return f'/{user_pk}/{project_pk}/performances/{self.pk}'

def delete(self):
# delete event will cascade to empty folder
Expand Down

0 comments on commit 395307c

Please sign in to comment.