django-storages: S3Boto3Storage raises ValueError: I/O operation on closed file.
When running python manage.py collectstatic
we get the following exception:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "~/venvs/app_root/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "items()venvs/app_root/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "~/venvs/app_root/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "~/venvs/app_root/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "~/venvs/app_root/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 199, in handle
collected = self.collect()
File "~/venvs/app_root/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 139, in collect
for original_path, processed_path, processed in processor:
File "~/venvs/app_root/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 246, in post_process
for name, hashed_name, processed, _ in self._post_process(paths, adjustable_paths, hashed_files):
File "~/venvs/app_root/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 312, in _post_process
hashed_name = self.hashed_name(name, content_file)
File "~/venvs/app_root/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 109, in hashed_name
file_hash = self.file_hash(clean_name, content)
File "~/venvs/app_root/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 86, in file_hash
for chunk in content.chunks():
File "~/venvs/app_root/lib/python3.5/site-packages/django/core/files/base.py", line 76, in chunks
self.seek(0)
ValueError: I/O operation on closed file.
This only happens when using django-storages 1.6.4 or above. Versions 1.6.3 and lower work fine.
We’re using Django 1.11.4, python 3.5.2, boto3 1.4.6.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 18
- Comments: 73 (20 by maintainers)
Commits related to this issue
- 🐛(backend) workaround allowing to use newer version of django-storages Django-storages version we use has a security issue but it was impossible to upgrade it because newer version are not compatibl... — committed to openfun/marsha by lunika 5 years ago
- 🐛(backend) workaround allowing to use newer version of django-storages Django-storages version we use has a security issue but it was impossible to upgrade it because newer version are not compatibl... — committed to openfun/marsha by lunika 5 years ago
- 🐛(backend) workaround allowing to use newer version of django-storages Django-storages version we use has a security issue but it was impossible to upgrade it because newer version are not compatibl... — committed to openfun/marsha by lunika 5 years ago
- 🐛(backend) workaround allowing to use newer version of django-storages Django-storages version we use has a security issue but it was impossible to upgrade it because newer version are not compatibl... — committed to openfun/marsha by lunika 5 years ago
- Add fix for boto3 file seek error Fixes error: ``` File "/var/www/mvsim/releases/blue/mvsim/ve/lib/python3.6/site-packages/django/core/files/base.py", line 76, in chunks self.seek(0) ... — committed to ccnmtl/django-cacheds3storage by nikolas 5 years ago
- Fix file seek error in boto3 - closes #382 This is the solution from @charlesthk here: https://github.com/jschneier/django-storages/issues/382#issuecomment-377174808 — committed to nikolas/django-storages by nikolas 5 years ago
- S3Boto3: Fix ValueError I/O operation on closed file (#754) This commit fixed a bug in Django that was addressed in the 1.10 release. It then had the knock on effects of causing #382. Since this libr... — committed to jschneier/django-storages by jschneier 5 years ago
- Merge #1984 1984: Scheduled weekly dependency update for week 37 r=rehandalal a=pyup-bot ### Update [botocore](https://pypi.org/project/botocore) from **1.12.224** to **1.12.228**. <details> ... — committed to mozilla/normandy by bors[bot] 5 years ago
- 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 - https://g... — committed to ccnmtl/django-cacheds3storage by nikolas 4 years ago
- 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 - https://g... — committed to ccnmtl/django-cacheds3storage by nikolas 4 years ago
- Fix "ValueError: seek of closed file" bug - closes #382 This is @mannpy's solution from: https://github.com/jschneier/django-storages/issues/382#issuecomment-592876060 — committed to nikolas/django-storages by nikolas 4 years ago
- Fix "ValueError: I/O operation on closed file" with S3 in django-storages==1.6.5, te S3Boto3Storage backend closes the file after saving. This effectively prevents from re-opening the same file later... — committed to overhangio/openedx-scorm-xblock by regisb 4 years ago
Using a custom StorageClass fix the issue :
@pasevin Thank you! It doesn’t work because the method doesn’t return a result from the parent class. @voiddragon Here is the fixed version:
@jschneier Can you please specify what tag/release this is fixed in?
I still experience this issue in django-storages 1.7.1 .
Would it be possible to get a new release out?
Thank you to the kind souls who update this page. I visit after every major Django update to grab the latest workaround. You don’t have unit tests, CI, or release notes, but like migrating birds we know where to come. In the spirit of all the duct tape that holds the internet together, Bravo.
@charlesthk can you make a pull request with this to the master?
This looks like a better solution, can we possibly have a patch for this and new version? I know it’s because of
s3transfer
now, but this could be nice to have indjango-storages
.For me, a patch like this (which does not involve copying) fixes the issues.
Thanks! This fix works for django-storages version 1.9.1.
this issue should be reopened. I still get ValueError: I/O operation on closed file. And the custom class https://github.com/jschneier/django-storages/issues/382#issuecomment-592876060 solves it.
Pull request with @charlesthk’s solution is here: https://github.com/jschneier/django-storages/pull/717
@abhinavnair thx, downgrade to 1.5.0 helps
I just tried going back versions even further and at 1.5.0, this issue doesn’t occur. It occurs on every version later than 1.5.0. Moreover this issue only arises when I am trying to upload an image file after cropping it. For all other cases, there is no problem at all.
Here is a snippet for uploading a cropped image file:-
Pull request opened for this fix here: https://github.com/jschneier/django-storages/pull/905
@voiddragon
Here’s rewritten custom class:
+1 on this, I also used charlesthk solution temporarily and it worked with: