Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以使用django_compressor/S3/gzip?_Django_Django Compressor - Fatal编程技术网

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

是否可以使用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

如何使用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'

#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
  • 如果你的S3在我们之外。您需要创建一个自定义的
    S3Connection
    类,将
    DefaultHost
    变量覆盖到您的s3url中。示例
    s3-eu-west-1.amazonaws.com
  • 如果您使用的是虚线bucket名称,例如
    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