How do I use Django, sorl-thumbnail, memcached, and S3 together?

前端 未结 1 595
梦如初夏
梦如初夏 2021-02-06 11:25

I\'ve got a project in which I need to start creating dynamically-resized thumbnails of user-uploaded images, where previously I\'d been generating some specifically sized ones.

相关标签:
1条回答
  • 2021-02-06 12:20

    You might want to look at Django-storages it was a nice AWS S3 that would fix the issue for you.Along with that also install boto, as django-storage has a dependency on boto.

    Then you will have to add the following into your settings.py

    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/'
    

    Then your cache will be stored on AWS S3 itself.

    I hope it helps

    Note: for security reasons its a good idea to add your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables instead of just writing them down in setting.py directly.

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