I configured my Heroku/Django app to serve media files from S3 using this tutorial a while back. I only cared about media files on S3 at the time, so I didn't use a custom storage originally, and stored the files in the root of my S3 bucket. But it's working.
However, now I want to store my static files on S3 because I have a few static videos and they're larger than I want to serve from my dyno. But when I configure the static files according to the tutorial, they still serve from the dyno. Here's my settings:
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'storages',
...
]
...
# Django storages configuration
AWS_STORAGE_BUCKET_NAME = os.environ.get('S3_BUCKET')
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_AUTO_CREATE_BUCKET = False
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_S3_ENCRYPTION = True
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'static'
# Static file storage
STATICFILES_STORAGE = 'core.custom_storages.StaticStorage'
STATICFILES_LOCATION = 'static'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = 'https://{}/{}/'.format(AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
)
# Media file storage
DEFAULT_FILE_STORAGE = 'core.custom_storages.MediaStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media')
...
And here's custom_storages.py:
$ cat core/custom_storages.py
# custom_storages.py
from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage
class StaticStorage(S3Boto3Storage):
location = 'static'
class MediaStorage(S3Boto3Storage):
location = ''
Again, to be clear, media storage is working and serves from the s3 bucket correctly. So it's really puzzling to me that the static files aren't.
EDIT: When I push to Heroku, I'm seeing this in the logs, which indicates to me that I'm not even copying to S3:
remote: -----> $ python manage.py collectstatic --noinput
remote: 2090 static files copied to '/tmp/build_a69320678067fe5b2ce29ed74018de75/core/staticfiles', 2188 post-processed.