S3 not serving media files to Django heroku app
-
06-07-2021 - |
Question
Good night fellas, I'm trying to setup a app with django on Heroku using S3 as a server for my javascript and my stylesheet. Following some posts, I configured a bucket on S3 and configured my env vars into heroku. Then in my settings file I have something like this
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'apuh',
'storages',
)
AWS_QUERYSTRING_AUTH = False
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 = 'mybucket'
STATIC_URL = 'https://s3.amazonaws.com/%s/' % AWS_STORAGE_BUCKET_NAME ADMIN_MEDIA_PREFIX = '/static/admin/'
When I run my app, it crashes with this error:
Error: No module named storages
I installed django_storages with pip install django_storages. It's installed on my enviroment. If I comment the 'storages' line it wont load any of the files from the bucket, I guess that the request doesn't send the propers tokens for authentication.
So, my question is, what I'm doing wrong. I have checked questions and questions but looks like I'm missing something big.
Thanks in advance
Solution
maybe adding these would help?
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/'
and yes installing boto aswell
and then run pip freeze > requirements.txt just to make sure if all your dependencies are up to date
I hope it helps