2

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.
kerkeslager
  • 1,364
  • 4
  • 17
  • 34
  • Check that your Config Vars are set and pointing to the S3 bucket in the Heroku dashboard and that the IAM user whos access key and secret are in the config var has write authorization to the bucket. – Chuck LaPress Oct 07 '18 at 04:29
  • reach out if you still can't get it to work, the settings config in that tutorial are slightly different then what has worked for me, I'll be happy to look at your settings. – Chuck LaPress Oct 07 '18 at 04:43

2 Answers2

3

I also experienced this problem.

When I ran my site on local host the images and static content loaded correctly from my S3 bucket, however it did not work from Heroku.

I had imported import django_heroku in my settings file - this did not fix the problem by itself.

The solution:

  • I needed to add staticfiles=False argument to django_heroku.settings(locals()) in my settings.py file, like below:

django_heroku.settings(locals(), staticfiles=False)

  • Then run python manage.py collectstatic from the heroku shell
femeloper
  • 96
  • 7
2

You should see https://stackoverflow.com/a/57049235/11652661

if you are using "import django_heroku" in your settings.py

nofoobar
  • 2,826
  • 20
  • 24