[Django]-How to make django uploaded images to display in CloudFront frontend + Beanstalk Backend

3👍

Take a look at using django-storages to save your uploads. I use S3 for storing uploads of a django/docker/EB deployment, and include django settings that look something like this (I keep them in settings/deployment.py):

if 'AWS_ACCESS_KEY_ID' in os.environ:
    # Use Amazon S3 for storage for uploaded media files
    # Keep them private by default
    DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
    # Amazon S3 settings.
    AWS_ACCESS_KEY_ID = os.environ["AWS_ACCESS_KEY_ID"]
    AWS_SECRET_ACCESS_KEY = os.environ["AWS_SECRET_ACCESS_KEY"]
    AWS_STORAGE_BUCKET_NAME = os.environ["AWS_STORAGE_BUCKET_NAME"]
    AWS_S3_REGION_NAME = os.environ.get("AWS_S3_REGION_NAME", None)
    AWS_S3_SIGNATURE_VERSION = 's3v4'
    AWS_AUTO_CREATE_BUCKET = False
    AWS_HEADERS = {"Cache-Control": "public, max-age=86400"}
    AWS_S3_FILE_OVERWRITE = False
    AWS_DEFAULT_ACL = 'private'
    AWS_QUERYSTING_AUTH = True
    AWS_QUERYSTRING_EXPIRE = 600
    AWS_S3_SECURE_URLS = True
    AWS_REDUCED_REDUNDANCY = False
    AWS_IS_GZIPPED = False

    MEDIA_ROOT = '/'
    MEDIA_URL = 'https://s3.{}.amazonaws.com/{}/'.format(
        AWS_S3_REGION_NAME, AWS_STORAGE_BUCKET_NAME)
    USING_AWS = True
👤Ben

Leave a comment