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

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)

Mar*_*rat 8

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

AWS_IS_GZIPPED = True
Run Code Online (Sandbox Code Playgroud)


Mar*_*ind 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在你的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)