是否可以使用django_compressor/S3/gzip?
如何使用django_压缩器将gzip文件发送到AmazonS3 我试过几种方法,但都没用。这是我最后的settings.py配置:是否可以使用django_compressor/S3/gzip?,django,django-compressor,Django,Django Compressor,如何使用django_压缩器将gzip文件发送到AmazonS3 我试过几种方法,但都没用。这是我最后的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' #COMP
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'
和mystorage.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
S3存储后端。添加到settings.py:
AWS_IS_GZIPPED = True
经过几天的努力和研究,我终于能够做到这一点 基本上你需要做几件事:
AWS\u IS\u gzip=True
S3Connection
类,将DefaultHost
变量覆盖到您的s3url中。示例s3-eu-west-1.amazonaws.com
subdomain.domain.tld
。您需要设置AWS\u S3\u CALLING\u FORMAT='boto.S3.connection.OrdinaryCallingFormat'
cacheds3botostrage
cacheds3botostrage
类:
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
2019年更新:在 以及您的设置:
#settings.py
INSTALLED_APPS += ['compressor']
AWS_IS_GZIPPED = True
STATIC_ROOT = '/path/to/staticfiles' #if not set, set this to an empty folder
COMPRESS_ROOT = STATIC_ROOT
STATICFILES_STORAGE = 'mysite.storage.CachedS3BotoStorage'
COMPRESS_STORAGE = STATICFILES_STORAGE
STATIC_URL = 'https://compressor-test.s3.amazonaws.com/'
COMPRESS_URL = STATIC_URL
您可以简单地运行:
python manage.py collectstatic
我正在研究同一个问题,刚刚在django_压缩机的GitHub上提交了一个问题:祝你好运@kcharvey!我期待下一个版本:-DAnyone排序这个?对于cloudfront用户来说,这还不足以让cloudfront提供Gzip资产这是正确的答案。另见
python manage.py collectstatic