How to set-up a Django project with django-storages and Amazon S3, but with different folders for static files and media files?

后端 未结 5 1480
情歌与酒
情歌与酒 2020-11-28 00:47

I\'m configuring a Django project that were using the server filesystem for storing the apps static files (STATIC_ROOT) and user uploaded files (MEDIA_ROO

5条回答
  •  天命终不由人
    2020-11-28 01:24


    File: PROJECT_NAME/custom_storages.py

    from django.conf import settings
    from storages.backends.s3boto import S3BotoStorage
    
    class StaticStorage(S3BotoStorage):
        location = settings.STATICFILES_LOCATION
    
    class MediaStorage(S3BotoStorage):
        location = settings.MEDIAFILES_LOCATION
    

    File: PROJECT_NAME/settings.py

    STATICFILES_LOCATION = 'static'
    MEDIAFILES_LOCATION = 'media'
    
    if not DEBUG:
        STATICFILES_STORAGE = 'PROJECT_NAME.custom_storages.StaticStorage'
        DEFAULT_FILE_STORAGE = 'PROJECT_NAME.custom_storages.MediaStorage'
        AWS_ACCESS_KEY_ID = 'KEY_XXXXXXX'
        AWS_SECRET_ACCESS_KEY = 'SECRET_XXXXXXXXX'
        AWS_STORAGE_BUCKET_NAME = 'BUCKET_NAME'
        AWS_HEADERS = {'Cache-Control': 'max-age=86400',}
        AWS_QUERYSTRING_AUTH = False
    

    And run: python manage.py collectstatic

提交回复
热议问题