5

We're going to start using S3 to host our static AND media files.

Does anyone have a good link that describes how to do both with wagtail?

We're on wagtail 1.9.

I can't get both of them to work at the same time.

https://wagtail.io/blog/amazon-s3-for-media-files/

Any help greatly appreciated.

vandekerkoff
  • 415
  • 8
  • 24
  • Do you want to use the same S3 bucket or 2 different ones? When you say at the same time do you mean that you can make them work without each other? – Benos May 16 '17 at 23:52

2 Answers2

3

Thanks both for responding.

I've managed to work it out.

To be clear, I want to use the same bucket in S3 to serve my static and my media files for the wagtail site.

We're using docker containers with FROM python:2.7

  • wagtail==1.9
  • django-storages==1.5.2
  • boto3==1.4.4

custom_storages.py

from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage
class StaticStorage(S3Boto3Storage):
  location = settings.STATICFILES_LOCATION
class MediaStorage(S3Boto3Storage):
  location = settings.MEDIAFILES_LOCATION

settings file

STATICFILES_LOCATION = 'static'
MEDIAFILES_LOCATION = 'media'
STATICFILES_STORAGE = 'pcstudents.custom_storages.StaticStorage'
DEFAULT_FILE_STORAGE = 'pcstudents.custom_storages.MediaStorage'
COMPRESS_STORAGE = STATICFILES_STORAGE
AWS_S3_OBJECT_PARAMETERS = {
  'CacheControl': 'max-age=86400',
}
AWS_S3_REGION_NAME = 'region'
AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_QUERYSTRING_AUTH = False
AWS_STORAGE_BUCKET_NAME = 'bucketname'
AWS_ACCESS_KEY_ID = 'secrets'
AWS_SECRET_ACCESS_KEY = 'moresecrets'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_PRELOAD_METADATA = True
STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
MEDIA_URL = "https://%s/media/" % AWS_S3_CUSTOM_DOMAIN
COMPRESS_ROOT = ''
STATICFILES_FINDERS = [
  'django.contrib.staticfiles.finders.FileSystemFinder',
  'django.contrib.staticfiles.finders.AppDirectoriesFinder',
  'compressor.finders.CompressorFinder',
]
STATICFILES_DIRS = [
  '/code/static',
  '/usr/local/lib/python2.7/site-packages/wagtail/wagtailadmin/static/wagtailadmin',
]

With this setup I end up with an S3 bucket, with two folders in, static and media.

I can collectstatic into the S3 static folder, and upload and download from/to the media folder.

If anyone can see any way to improve that I'm all ears, but that does work.

Matt

vandekerkoff
  • 415
  • 8
  • 24
  • 1
    This is completely according to Django's best practices, so I don't think your setup needs any improving at all – dentemm May 17 '17 at 13:41
  • 1
    I followed this answer to get S3 serving static and uploaded files correctly. Then when attempting to add cache busting via `ManifestStaticFilesStorage` ran into weird issues with duplicate files (specifically CSS files). I asked a question and have detailed the resolution over there - hope this comment saves someone a few hours! https://stackoverflow.com/questions/52309821/collectstatic-incorrectly-creates-multiple-css-files-in-s3/52323409#52323409 – Jonny Sep 14 '18 at 00:26
0

This blog post on wagtail.io helped me a lot. But what issues are you facing? Can you get it to work separately for both media and static files?

dentemm
  • 6,231
  • 3
  • 31
  • 43