Skip to content

Commit 8101408

Browse files
committed
Update "seek of closed file" fix for current django-storages
Using this class with the new django-storages gives the error: ValueError: seek of closed file This change should fix that - jschneier/django-storages#382 (comment)
1 parent dbdf5c1 commit 8101408

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

CHANGES

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
0.2.1 (2019-06-20)
1+
0.2.2
22
==================
3+
* Update the seek of closed file fix to work with django-storages 1.9.x.
34

5+
0.2.1 (2019-06-20)
6+
==================
47
* Add boto3 file seek fix
58

69
0.2.0 (2019-06-19)

cacheds3storage/__init__.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,31 @@ def __init__(self, *args, **kwargs):
1111
self.local_storage = get_storage_class(
1212
'compressor.storage.CompressorFileStorage')()
1313

14-
# Fix for "ValueError: seek of closed file" problem with boto3
15-
# https://github.com/jschneier/django-storages/issues/382#issuecomment-377174808
16-
def _save_content(self, obj, content, parameters):
14+
# https://github.com/jschneier/django-storages/issues/382
15+
# #issuecomment-592876060
16+
def _save(self, name, content):
1717
"""
18-
We create a clone of the content file as when this is passed to boto3 it wrongly closes
19-
the file upon upload where as the storage backend expects it to still be open
18+
We create a clone of the content file as when this is passed
19+
to boto3 it wrongly closes the file upon upload where as the
20+
storage backend expects it to still be open
2021
"""
2122
# Seek our content back to the start
2223
content.seek(0, os.SEEK_SET)
2324

24-
# Create a temporary file that will write to disk after a specified size
25-
content_autoclose = SpooledTemporaryFile()
25+
# Create a temporary file that will write to disk after a
26+
# specified size. This file will be automatically deleted when
27+
# closed by boto3 or after exiting the `with` statement if the
28+
# boto3 is fixed
29+
with SpooledTemporaryFile() as content_autoclose:
2630

27-
# Write our original content into our copy that will be closed by boto3
28-
content_autoclose.write(content.read())
31+
# Write our original content into our copy that will be
32+
# closed by boto3
33+
content_autoclose.write(content.read())
2934

30-
# Upload the object which will auto close the content_autoclose instance
31-
super(CachedS3BotoStorage, self)._save_content(obj, content_autoclose, parameters)
32-
33-
# Cleanup if this is fixed upstream our duplicate should always close
34-
if not content_autoclose.closed:
35-
content_autoclose.close()
35+
# Upload the object which will auto close the
36+
# content_autoclose instance
37+
return super(CachedS3BotoStorage, self)._save(
38+
name, content_autoclose)
3639

3740
def save(self, name, content):
3841
name = super(CachedS3BotoStorage, self).save(name, content)

0 commit comments

Comments
 (0)