Is it possible to use django_compressor/S3/gzip?

ぐ巨炮叔叔 提交于 2019-11-29 14:05:45

问题


How is it possible to use django_compressor to send gziped files to Amazon S3?

I tried in several ways but it didn't work. Here is my last settings.py configuration:

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'

and my 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

回答1:


django-storages S3 storage backend supports gzip. Add to settings.py:

AWS_IS_GZIPPED = True



回答2:


After plenty of days of hard work and research I was finally able to do this.

Basically you need to do a few things:

  1. Use AWS_IS_GZIPPED = True
  2. If your S3 is outside of US. You need to create a custom S3Connection class where you override the DefaultHost variable to your S3 url. Example s3-eu-west-1.amazonaws.com
  3. If you're using a dotted bucket name, example subdomain.domain.tld. You need to set AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
  4. You have to set non_gzipped_file_content = content.file in your CachedS3BotoStorage

This is the CachedS3BotoStorage class you need:

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



回答3:


Update 2019: it's described in the 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

And your settings:

#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

Than you can simply run:

python manage.py collectstatic


来源:https://stackoverflow.com/questions/15177801/is-it-possible-to-use-django-compressor-s3-gzip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!