Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for already gzipped content to S3 storage #122

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion storages/backends/s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,15 +390,19 @@ def _save(self, name, content):
cleaned_name = self._clean_name(name)
name = self._normalize_name(cleaned_name)
headers = self.headers.copy()
_type, encoding = mimetypes.guess_type(name)
content_type = getattr(content, 'content_type',
mimetypes.guess_type(name)[0] or self.key_class.DefaultContentType)
_type or self.key_class.DefaultContentType)

# setting the content_type in the key object is not enough.
headers.update({'Content-Type': content_type})

if self.gzip and content_type in self.gzip_content_types:
content = self._compress_content(content)
headers.update({'Content-Encoding': 'gzip'})
elif encoding:
# If the content already has a particular encoding, set it
headers.update({'Content-Encoding': encoding})

content.name = cleaned_name
encoded_name = self._encode_name(name)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ def test_storage_save(self):
rewind=True
)

def test_storage_save_gzipped(self):
"""
Test saving a gzipped file
"""
name = 'test_storage_save.gz'
content = ContentFile("I am gzip'd")
self.storage.save(name, content)
key = self.storage.bucket.get_key.return_value
key.set_metadata.assert_called_with('Content-Type',
'application/octet-stream')
key.set_contents_from_file.assert_called_with(
content,
headers={'Content-Type': 'application/octet-stream',
'Content-Encoding': 'gzip'},
policy=self.storage.default_acl,
reduced_redundancy=self.storage.reduced_redundancy,
rewind=True,
)

def test_storage_save_gzip(self):
"""
Test saving a file with gzip enabled.
Expand Down