@@ -330,8 +330,16 @@ def _save(self, name, content):
330
330
else :
331
331
content .seek (0 )
332
332
# 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
335
343
# Close all temp files.
336
344
for temp_file in temp_files :
337
345
temp_file .close ()
@@ -525,14 +533,6 @@ class ManifestStaticS3Storage(ManifestFilesMixin, StaticS3Storage):
525
533
"AWS_S3_MAX_AGE_SECONDS_CACHED" : 60 * 60 * 24 * 365 , # 1 year.
526
534
})
527
535
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
-
536
536
def post_process (self , * args , ** kwargs ):
537
537
initial_aws_s3_max_age_seconds = self .settings .AWS_S3_MAX_AGE_SECONDS
538
538
self .settings .AWS_S3_MAX_AGE_SECONDS = self .settings .AWS_S3_MAX_AGE_SECONDS_CACHED
0 commit comments