是否可以使用django_compressor / S3 / gzip?

时间:2013-03-02 18:21:25

标签: 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'

和我的 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

3 个答案:

答案 0 :(得分:7)

django-storages S3存储后端supports gzip。添加到settings.py:

AWS_IS_GZIPPED = True

答案 1 :(得分:5)

经过大量的努力和研究,我终于能够做到这一点。

基本上你需要做一些事情:

  1. 使用AWS_IS_GZIPPED = True
  2. 如果您的S3不在美国境内。您需要创建一个自定义S3Connection类,在其中将DefaultHost变量覆盖到您的S3网址。示例s3-eu-west-1.amazonaws.com
  3. 如果您使用的是虚线桶名称,例如subdomain.domain.tld。您需要设置AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
  4. 您必须在non_gzipped_file_content = content.file
  5. 中设置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
    

答案 2 :(得分:0)

2019年更新:official documentation

中对此进行了描述
#mysite.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):
        self.local_storage._save(name, content)
        super(CachedS3BotoStorage, self).save(name, self.local_storage._open(name))
        return name

以及您的设置:

#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