From 0808e3194aab3e44cf95050e25bf63d66ed6213e Mon Sep 17 00:00:00 2001 From: Dave Hall Date: Sun, 21 Aug 2022 13:00:38 +0100 Subject: [PATCH] Alternative fix for #141 --- django_s3_storage/storage.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/django_s3_storage/storage.py b/django_s3_storage/storage.py index 7df2190..374d125 100644 --- a/django_s3_storage/storage.py +++ b/django_s3_storage/storage.py @@ -330,8 +330,13 @@ def _save(self, name, content): else: content.seek(0) # Save the file. - self.s3_connection.upload_fileobj(content, put_params.pop('Bucket'), put_params.pop('Key'), + original_close = content.close + content.close = lambda: None + try: + self.s3_connection.upload_fileobj(content, put_params.pop('Bucket'), put_params.pop('Key'), ExtraArgs=put_params, Config=self._transfer_config) + finally: + content.close = original_close # Close all temp files. for temp_file in temp_files: temp_file.close() @@ -525,14 +530,6 @@ class ManifestStaticS3Storage(ManifestFilesMixin, StaticS3Storage): "AWS_S3_MAX_AGE_SECONDS_CACHED": 60 * 60 * 24 * 365, # 1 year. }) - def _save(self, name, content): - # See: https://github.com/etianen/django-s3-storage/issues/141 - # Fix adapted from: https://github.com/jschneier/django-storages/pull/968 - content.seek(0) - with self.new_temporary_file() as tmp: - shutil.copyfileobj(content, tmp) - return super()._save(name, File(tmp)) - def post_process(self, *args, **kwargs): initial_aws_s3_max_age_seconds = self.settings.AWS_S3_MAX_AGE_SECONDS self.settings.AWS_S3_MAX_AGE_SECONDS = self.settings.AWS_S3_MAX_AGE_SECONDS_CACHED