Serving static and media files from S3 wagtail

限于喜欢 提交于 2019-12-05 21:04:57

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?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!