Search code examples
pythondjangoamazon-s3django-oscardjango-storage

Django app does not load images from AWS bucket's media folder


I'm using django-oscar,and wanted to serve my static files with AWS S3. To config my s3 bucket I've created a module called aws with conf.py and utils.py files.

On my website when I upload an image to the product it gets uploaded well with the correct path to my aws s3 bucket, but then after very short time the path changes from https://mybucketname.s3.amazonaws.com/media/cache/..../image.jpg to https://mybucketname.s3.amazonaws.com/cache/..../image.jpg

The images are in the media folder in my bucket.

I'm hosting my web app on heroku, the static files are served correctly but the issue happen in media folder.

Here is my code -

utils.py file

from storages.backends.s3boto3 import S3Boto3Storage

StaticRootS3BotoStorage = lambda: S3Boto3Storage(location='static')
MediaRootS3BotoStorage  = lambda: S3Boto3Storage(location='media')

as static and media are the folders on my s3 bucket

conf.py

import datetime

AWS_ACCESS_KEY_ID = "xxx"
AWS_SECRET_ACCESS_KEY = "yyy"

AWS_PRELOAD_METADATA = True
AWS_QUERYSTRING_AUTH = False
AWS_DEFAULT_ACL = None 

DEFAULT_FILE_STORAGE = 
'myproject.aws.utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 
'myproject.aws.utils.StaticRootS3BotoStorage'
AWS_STORAGE_BUCKET_NAME = 'mybucket-name'
S3DIRECT_REGION = 'us-east-2'
S3_URL = '//%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = '//%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
STATIC_URL = S3_URL + 'static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

two_months = datetime.timedelta(days=61)
date_two_months_later = datetime.date.today() + two_months
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00 
GMT")

AWS_HEADERS = { 
    'Expires': expires,
    'Cache-Control': 'max-age=%d' % 
    (int(two_months.total_seconds()), ),
}

and my settings.py I added this

from myproject.aws.conf import *

What should I do to resolve this issue?


Solution

  • The file storage system configured for your Django app should be a class that implements django.core.files.storage.Storage [1]

    storages.backends.s3boto3.S3Boto3Storage already implements this storage interface. [2]

    Setting StaticRootS3BotoStorage in utils.py to a lambda, the Storage system is instantiated lazily with the proper location value; but the location attribute in the storage class itself is never changes. [3]

    location = setting('AWS_LOCATION', '')
    

    Django clears properties of storage instance when the project settings changes. [4] So that when the location attribute is resolved on the storage system, it effectively looks up the class attribute one (location value is shown in above snippet) because location attribute is missing in the instance.

    This situation can be solved by subclassing storages.backends.s3boto3.S3Boto3Storage instead. This guarantees that location value never changes regardless of changes to project settings.

    class StaticRootS3BotoStorage(S3Boto3Storage):
        location = 'static'
    
    class MediaRootS3BotoStorage(S3Boto3Storage):
        location = 'media'