我正在寻找使用django-storages(AWS S3)使用django上传gzip压缩媒体文件的解决方案。
我的settings.py为了实现这一点:
# Django Storage - Amazon S3
AWS_QUERYSTRING_AUTH = False
AWS_IS_GZIPPED = True
AWS_PRELOAD_METADATA = True
GZIP_CONTENT_TYPES = (
'text/css',
'text/csv',
'text/html',
'text/javascript',
'text/plain',
'text/xml',
'text/x-markdown',
'application/javascript',
'application/x-javascript',
'application/json',
'application/pdf',
'application/font-woff',
'application/octet-stream',
'font/opentype',
'application/xml',
'image/png',
'image/jpeg',
'image/pjpeg',
'image/svg+xml',
'image/gif',
'image/jpg',
'video/mp4',
'video/ogg',
'video/avi',
'video/mpeg',
'video/webm',
)
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY', '')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_KEY', '')
AWS_HEADERS = {
'Expires': 'Thu, 1 Apr 2020 20:00:00 GMT',
'Cache-Control': 'max-age=2592000',
}
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME', '')
DEFAULT_FILE_STORAGE = 'dgnest.s3utils.MediaRootS3BotoStorage'
MEDIA_URL = 'http://s3.amazonaws.com/%s/media/' % AWS_STORAGE_BUCKET_NAME
STATICFILES_STORAGE = 'dgnest.s3utils.StaticRootS3BotoStorage'
STATIC_URL = 'http://s3.amazonaws.com/%s/static/' % AWS_STORAGE_BUCKET_NAME
我的档案存储:
from storages.backends.s3boto import S3BotoStorage
from django.contrib.staticfiles.storage import CachedFilesMixin
class CachedStaticS3BotoStorage(CachedFilesMixin, S3BotoStorage):
"""Extends S3BotoStorage to save static files with hashed filenames."""
pass
class MediaS3BotoStorage(S3BotoStorage):
"""Extends S3BotoStorage to save media files."""
# gzip = False
pass
StaticRootS3BotoStorage = lambda: CachedStaticS3BotoStorage(location='static')
MediaRootS3BotoStorage = lambda: MediaS3BotoStorage(location='media')
我的静态文件经过哈希处理和gzip压缩。我只是挣扎着媒体文件似乎他们被gzip压缩并上传到亚马逊s3但django无法处理'cStringIO.StringO'对象。这是我尝试在我的某个具有图像或文件字段的模型中发布帖子时出现的错误。
Django Version: 1.6.4
Exception Type: AttributeError
Exception Value:
'cStringIO.StringO' object has no attribute 'size'
Exception Location: /home/oscar/.virtualenvs/dgnest/local/lib/python2.7/site-packages/django/db/models/fields/files.py in _get_size, line 70
希望能找到一些帮助。谢谢。