Django + Heroku + S3

前端 未结 1 1186
孤独总比滥情好
孤独总比滥情好 2020-12-25 10:16

I\'ve been trying to research ways to make AWS S3 work with Heroku for FileField and ImageField uploads. But I\'ve been failing to get it working.

The plan is to

相关标签:
1条回答
  • 2020-12-25 10:54

    Use Django Storages for managing static files on S3. Then follow Heroku Static assets guide when deploying.

    First, create a bucket in S3, using either the AWS Console or your favorite tool. Then, modify your settings.py and add the following values:

    import os
    
    AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
    AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
    AWS_STORAGE_BUCKET_NAME = '<YOUR BUCKET NAME>'
    
    STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
    DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
    
    STATIC_URL = 'http://' + AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/'
    ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
    

    Notice that we are using environment variables to store the AWS access key and secret key. While we are on this topic, if you are planning to open source the Django application you are deploying, consider also storing your SECRET_KEY in an environment variable.

    The above is from here

    0 讨论(0)
提交回复
热议问题