Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File is closed after s3 upload #141

Closed
stumitchell opened this issue Jun 29, 2022 · 9 comments · Fixed by #142
Closed

File is closed after s3 upload #141

stumitchell opened this issue Jun 29, 2022 · 9 comments · Fixed by #142

Comments

@stumitchell
Copy link

hi I am seeing this issue with your library

jschneier/django-storages#382

@stumitchell
Copy link
Author

stumitchell commented Jun 29, 2022

class CustomS3Storage(S3Storage):

    def _save(self, name, content):
        """
        We create a clone of the content file as when this is passed to boto3 it wrongly closes
        the file upon upload where as the storage backend expects it to still be open
        """
        # Seek our content back to the start
        content.seek(0, os.SEEK_SET)

        # Create a temporary file that will write to disk after a specified size
        content_autoclose = self.new_temporary_file()

        # Write our original content into our copy that will be closed by boto3
        content_autoclose.write(content.read())
        content_autoclose.file = content.file

        # Upload the object which will auto close the content_autoclose instance
        result = super()._save(name, content_autoclose)

        # Cleanup if this is fixed upstream our duplicate should always close
        if not content_autoclose.closed:
            content_autoclose.close()

        return result

storage = CustomS3Storage()

Seems to help

@etianen
Copy link
Owner

etianen commented Jul 10, 2022

I'll push out a patch release momentarily

@jakobkarlstrand
Copy link

I'll push out a patch release momentarily

Hi, is there a solution for this? I get ValueError I/O while uploading to S3

@etianen
Copy link
Owner

etianen commented Jul 19, 2022 via email

@jakobkarlstrand
Copy link

Actually, it worked now. However I now get an UnicodeDecodeError at /api/jour-reports

Traceback (most recent call last):
  File "/Users/jakobkarlstrand/Documents/Programming/weknowit/HaningeBostader/backend/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/jakobkarlstrand/Documents/Programming/weknowit/HaningeBostader/backend/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/jakobkarlstrand/Documents/Programming/weknowit/HaningeBostader/backend/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "/Users/jakobkarlstrand/Documents/Programming/weknowit/HaningeBostader/backend/venv/lib/python3.9/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/Users/jakobkarlstrand/Documents/Programming/weknowit/HaningeBostader/backend/venv/lib/python3.9/site-packages/rest_framework/response.py", line 70, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "/Users/jakobkarlstrand/Documents/Programming/weknowit/HaningeBostader/backend/venv/lib/python3.9/site-packages/rest_framework/renderers.py", line 100, in render
    ret = json.dumps(
  File "/Users/jakobkarlstrand/Documents/Programming/weknowit/HaningeBostader/backend/venv/lib/python3.9/site-packages/rest_framework/utils/json.py", line 25, in dumps
    return json.dumps(*args, **kwargs)
  File "/opt/homebrew/Cellar/python@3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 234, in dumps
    return cls(
  File "/opt/homebrew/Cellar/python@3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/opt/homebrew/Cellar/python@3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/Users/jakobkarlstrand/Documents/Programming/weknowit/HaningeBostader/backend/venv/lib/python3.9/site-packages/rest_framework/utils/encoders.py", line 50, in default
    return obj.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa2 in position 50: invalid start byte

@etianen
Copy link
Owner

etianen commented Jul 20, 2022 via email

@stumitchell
Copy link
Author

Hi I am still seeing this issue in version 0.13.7 I will continue to investigate

etianen added a commit that referenced this issue Aug 21, 2022
etianen added a commit that referenced this issue Aug 21, 2022
etianen added a commit that referenced this issue Aug 21, 2022
@etianen
Copy link
Owner

etianen commented Aug 21, 2022

@stumitchell Cab you let me know if this is fixed for you in the latest commit to the master branch?

@etianen etianen reopened this Aug 21, 2022
@jakobkarlstrand
Copy link

For me it was the following error:

I tried to return the file, or information of the file, in a response. Check if you are doing something similar

@etianen etianen closed this as completed Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants