Skip to content

Commit 5e69f29

Browse files
committed
Alternative fix for #141
1 parent 3804325 commit 5e69f29

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

django_s3_storage/storage.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,16 @@ def _save(self, name, content):
330330
else:
331331
content.seek(0)
332332
# Save the file.
333-
self.s3_connection.upload_fileobj(content, put_params.pop('Bucket'), put_params.pop('Key'),
334-
ExtraArgs=put_params, Config=self._transfer_config)
333+
# HACK: Patch the file object to prevent `upload_fileobj` from closing it.
334+
# https://github.com/boto/boto3/issues/929
335+
original_close = content.close
336+
content.close = lambda: None
337+
try:
338+
self.s3_connection.upload_fileobj(content, put_params.pop('Bucket'), put_params.pop('Key'),
339+
ExtraArgs=put_params, Config=self._transfer_config)
340+
finally:
341+
# Restore the original close method.
342+
content.close = original_close
335343
# Close all temp files.
336344
for temp_file in temp_files:
337345
temp_file.close()
@@ -525,14 +533,6 @@ class ManifestStaticS3Storage(ManifestFilesMixin, StaticS3Storage):
525533
"AWS_S3_MAX_AGE_SECONDS_CACHED": 60 * 60 * 24 * 365, # 1 year.
526534
})
527535

528-
def _save(self, name, content):
529-
# See: https://github.com/etianen/django-s3-storage/issues/141
530-
# Fix adapted from: https://github.com/jschneier/django-storages/pull/968
531-
content.seek(0)
532-
with self.new_temporary_file() as tmp:
533-
shutil.copyfileobj(content, tmp)
534-
return super()._save(name, File(tmp))
535-
536536
def post_process(self, *args, **kwargs):
537537
initial_aws_s3_max_age_seconds = self.settings.AWS_S3_MAX_AGE_SECONDS
538538
self.settings.AWS_S3_MAX_AGE_SECONDS = self.settings.AWS_S3_MAX_AGE_SECONDS_CACHED

0 commit comments

Comments
 (0)