Search code examples
djangoamazon-web-servicesamazon-s3boto3python-django-storages

How do I make media files display automatically in django using s3 as storage?


I configured my s3 settings like so:

DEFAULT_FILE_STORAGE = 'Bonychicken.storage_backends.MediaStorage'
AWS_STORAGE_BUCKET_NAME = 'bonychicken'
AWS_S3_REGION_NAME = 'us-east-2'
AWS_ACCESS_KEY_ID = '<my access key id>'
AWS_SECRET_ACCESS_KEY = '<my secret access key>'
AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com"
AWS_QUERYSTRING_AUTH = False
AWS_DEFAULT_ACL = None
STATIC_URL = '/static/'
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/media/"
MEDIA_ROOT = f"https://{AWS_S3_CUSTOM_DOMAIN}/media/"
AWS_IS_GZIPPED = True
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}

My storage class is:

from storages.backends.s3boto3 import S3Boto3Storage

class MediaStorage(S3Boto3Storage):
    bucket_name = 'bonychicken'
    location = 'media'
    file_overwrite = False

I added storages to my installed apps and boto3 is also installed.

CORS Configuration is:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>

block public access has been turned off. IAM user has permissions to the bucket.

The problem is, whenever I upload a file, it is uploaded to my s3 bucket but it doesn't show on the html page. Trying to access the image url from the source code yields:

<Error>
    <Code>AccessDenied</Code>
    <Message>Access Denied</Message>
    <RequestId><random letters here></RequestId>
    <HostId>
        <random letters here>
    </HostId>
</Error>

But if I go to my bucket settings and select actions and click on make public after marking the media files, everything starts working. This means for each image uploaded, I have to mark and repeat over and over again.

How do I solve this?


Solution

  • I think that you should change the default ACL in your AWS settings as shown in the docs.

    Just set the AWS_DEFAULT_ACL to AWS_DEFAULT_ACL='public-read'.