Tho*_*mas 10 django django-compressor
如何使用django_compressor将gziped文件发送到Amazon S3?
我尝试了几种方法,但它没有用.这是我上次的settings.py配置:
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_URL = "http://xxx.cloudfront.net/"
STATIC_URL = COMPRESS_URL
COMPRESS_OUTPUT_DIR = 'CACHE'
#COMPRESS_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
COMPRESS_STORAGE = 'core.storage.CachedS3BotoStorage'
STATICFILES_STORAGE = 'compressor.storage.GzipCompressorFileStorage'
COMPRESS_YUI_BINARY = 'java -jar contrib/yuicompressor-2.4.7/build/yuicompressor-2.4.7.jar'
COMPRESS_YUI_JS_ARGUMENTS = ''
COMPRESS_CSS_FILTERS = ['compressor.filters.yui.YUICSSFilter']
COMPRESS_JS_FILTERS = ['compressor.filters.yui.YUIJSFilter']
COMPRESS_CSS_HASHING_METHOD = 'hash'
Run Code Online (Sandbox Code Playgroud)
和我的storage.py
from django.core.files.storage import get_storage_class
from storages.backends.s3boto import S3BotoStorage
class CachedS3BotoStorage(S3BotoStorage):
"""
S3 storage backend that saves the files locally, too.
"""
def __init__(self, *args, **kwargs):
super(CachedS3BotoStorage, self).__init__(*args, **kwargs)
self.local_storage = get_storage_class(
"compressor.storage.CompressorFileStorage")()
def save(self, name, content):
name = super(CachedS3BotoStorage, self).save(name, content)
self.local_storage._save(name, content)
return name
Run Code Online (Sandbox Code Playgroud)
django-storages S3存储后端支持gzip.添加到settings.py:
AWS_IS_GZIPPED = True
Run Code Online (Sandbox Code Playgroud)
经过大量的努力和研究,我终于能够做到这一点.
基本上你需要做一些事情:
AWS_IS_GZIPPED = TrueS3Connection类,将DefaultHost变量覆盖到S3网址.例s3-eu-west-1.amazonaws.comsubdomain.domain.tld.你需要设置AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'non_gzipped_file_content = content.file在你的CachedS3BotoStorage这是CachedS3BotoStorage你需要的课程:
class CachedS3BotoStorage(S3BotoStorage):
"""
S3 storage backend that saves the files locally, too.
"""
connection_class = EUConnection
location = settings.STATICFILES_LOCATION
def __init__(self, *args, **kwargs):
super(CachedS3BotoStorage, self).__init__(*args, **kwargs)
self.local_storage = get_storage_class(
"compressor.storage.CompressorFileStorage")()
def save(self, name, content):
non_gzipped_file_content = content.file
name = super(CachedS3BotoStorage, self).save(name, content)
content.file = non_gzipped_file_content
self.local_storage._save(name, content)
return name
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1985 次 |
| 最近记录: |