Is it possible to use django_compressor/S3/gzip?

前端 未结 3 1048
遇见更好的自我
遇见更好的自我 2020-12-31 11:36

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

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 11:53

    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
    

提交回复
热议问题