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

Fix for file-like objects without names #368

Merged
merged 1 commit into from
Jul 26, 2017
Merged
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
12 changes: 12 additions & 0 deletions storages/backends/s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,18 @@ def _save(self, name, content):
if self.preload_metadata:
self._entries[encoded_name] = obj

# If both `name` and `content.name` are empty or None, your request
# can be rejected with `XAmzContentSHA256Mismatch` error, because in
# `django.core.files.storage.Storage.save` method your file-like object
# will be wrapped in `django.core.files.File` if no `chunks` method
# provided. `File.__bool__` method is Django-specific and depends on
# file name, for this reason`botocore.handlers.calculate_md5` can fail
# even if wrapped file-like object exists. To avoid Django-specific
# logic, pass internal file-like object if `content` is `File`
# class instance.
if isinstance(content, File):
content = content.file

self._save_content(obj, content, parameters=parameters)
# Note: In boto3, after a put, last_modified is automatically reloaded
# the next time it is accessed; no need to specifically reload it.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_storage_save(self):

obj = self.storage.bucket.Object.return_value
obj.upload_fileobj.assert_called_with(
content,
content.file,
ExtraArgs={
'ContentType': 'text/plain',
'ACL': self.storage.default_acl,
Expand All @@ -96,7 +96,7 @@ def test_storage_save_gzipped(self):
self.storage.save(name, content)
obj = self.storage.bucket.Object.return_value
obj.upload_fileobj.assert_called_with(
content,
content.file,
ExtraArgs={
'ContentType': 'application/octet-stream',
'ContentEncoding': 'gzip',
Expand Down