I've been attempting to set up a MEDIA_URL for my Heroku app, which is currently serving static files via STATIC_URL from Amazon S3. The static files are working fine, but when I attempt to add a MEDIA_URL in addition to the current STATIC_URL, the pages no longer render at all and the app ceases to work.
The current settings are:
AWS_STORAGE_BUCKET_NAME = 'bucketname'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
S3_URL = 'http://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = S3_URL
AWS_ACCESS_KEY_ID = 'KEY'
AWS_SECRET_ACCESS_KEY = 'SECRET_KEY'
When I add:
MEDIA_URL = S3_URL
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
that causes the issue. Specifically, the MEDIA_URL is problematic as when DEFAULT_FILE_STORAGE is deleted, it still has the same issue. But I'm trying to determine best how to serve user uploaded media through this unsuccessfully.
If anyone has any insight how best to achieve this, it would be most appreciated.
This solution works quite well, as described below.
Create a file called s3utils.py
in the same directory as settings.py
:
from storages.backends.s3boto import S3BotoStorage
StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')
MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media')
Then in settings.py
:
DEFAULT_FILE_STORAGE = 'myproyect.s3utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'myproyect.s3utils.StaticRootS3BotoStorage'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With