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 file seek error in boto3 - closes #382 #717

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions storages/backends/s3boto3.py
Expand Up @@ -509,6 +509,19 @@ def _save(self, name, content):
return cleaned_name

def _save_content(self, obj, content, parameters):
"""
We create a clone of the content file as when this is passed to boto3 it wrongly closes
the file upon upload where as the storage backend expects it to still be open
"""
# Seek our content back to the start
content.seek(0, os.SEEK_SET)

# Create a temporary file that will write to disk after a specified size
content_autoclose = SpooledTemporaryFile()

# Write our original content into our copy that will be closed by boto3
content_autoclose.write(content.read())

# only pass backwards incompatible arguments if they vary from the default
put_parameters = parameters.copy() if parameters else {}
if self.encryption:
Expand All @@ -518,8 +531,13 @@ def _save_content(self, obj, content, parameters):
if self.default_acl:
put_parameters['ACL'] = self.default_acl
content.seek(0, os.SEEK_SET)
nikolas marked this conversation as resolved.
Show resolved Hide resolved
# Upload the object which will auto close the content_autoclose instance
obj.upload_fileobj(content, ExtraArgs=put_parameters)

# Cleanup if this is fixed upstream our duplicate should always close
if not content_autoclose.closed:
content_autoclose.close()

def delete(self, name):
name = self._normalize_name(self._clean_name(name))
self.bucket.Object(self._encode_name(name)).delete()
Expand Down