vandekerkoff
vandekerkoff

Reputation: 435

Serving static and media files from S3 wagtail

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.

Upvotes: 5

Views: 2459

Answers (2)

vandekerkoff
vandekerkoff

Reputation: 435

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

Upvotes: 3

dentemm
dentemm

Reputation: 6379

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?

Upvotes: 0

Related Questions