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

2024-05-15 02:34:17 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用django_compressor将gzip文件发送到amazons3?在

我试了好几种方法,但都没用。这是我的最后一次设置.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'

我的存储.py

^{pr2}$

Tags: pytrueurljsstaticstoragerootcompressor
3条回答

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

以及您的设置:

^{pr2}$

你可以简单地运行:

python manage.py collectstatic

django-storagesS3存储后端supports gzip。添加到设置.py公司名称:

AWS_IS_GZIPPED = True

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

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

  1. 使用AWS_IS_GZIPPED = True
  2. 如果你的S3在我们外面。您需要创建一个自定义的S3Connection类,在这个类中,您可以将DefaultHost变量覆盖到s3url中。示例s3-eu-west-1.amazonaws.com
  3. 如果您使用的是虚线存储区名称,例如subdomain.domain.tld。您需要设置AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
  4. 您必须在CachedS3BotoStorage中设置non_gzipped_file_content = content.file

这是您需要的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

相关问题 更多 >

    热门问题