8👍
django-storages S3 storage backend supports gzip. Add to settings.py:
AWS_IS_GZIPPED = True
5👍
After plenty of days of hard work and research I was finally able to do this.
Basically you need to do a few things:
- Use
AWS_IS_GZIPPED = True
- If your S3 is outside of US. You need to create a custom
S3Connection
class where you override theDefaultHost
variable to your S3 url. Examples3-eu-west-1.amazonaws.com
- If you’re using a dotted bucket name, example
subdomain.domain.tld
. You need to setAWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
- You have to set
non_gzipped_file_content = content.file
in yourCachedS3BotoStorage
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
- What does 'name__iexact' mean in django model filters?
- How to avoid having idle connection timeout while uploading large file?
- Add method imports to shell_plus
- Passing Parameters to Django CreateView
- Determine if Django is running under the development server
2👍
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
👤ohlr
- Can't create django project using Windows command prompt
- Determine if Django is running under the development server
- How to store google oauth token in django : Storage or database
- How to make action logging in Django with Django Rest Framework
Source:stackexchange.com