从S3 Wagtail提供静态和媒体文件 [英] Serving static and media files from S3 wagtail

查看:121
本文介绍了从S3 Wagtail提供静态和媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将开始使用S3托管我们的静态和媒体文件.

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

有人能很好地说明如何使用w吗?

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

我们正在使用w 1.9.

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.

推荐答案

感谢双方的响应.

我设法解决了.

需要明确的是,我想在S3中使用相同的存储区来为wagtail网站提供我的静态文件和媒体文件.

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

我们正在使用带有FROM python:2.7的Docker容器

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

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

设置文件

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',
]

使用此设置,我最终得到一个S3存储桶,其中有两个文件夹,分别是静态文件夹和媒体文件夹.

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

我可以将static收集到S3静态文件夹中,并从媒体文件夹上载和下载.

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.

马特

这篇关于从S3 Wagtail提供静态和媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆