-
Notifications
You must be signed in to change notification settings - Fork 135
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-like objects are closed after uploading #80
Comments
Yeah looks like it will only happen for the non-multipart upload case because the provided file object is used. Will probably have to figure out a way to safeguard against accidentally closing the file. |
Any workaround to prevent the file being closed? I'm using this function with unnamed temporary files, so I can't simply reopen the file. I'd like to do something else with this file (for example, rotate it to local disk) in case upload_fileobj fails, but it closes the file.... (sigh) |
The workaround by leonsmith here worked for me- matthewwithanm/django-imagekit#391 |
Same issue here. I'm also using Imagekit.
|
I can't tell - is this a bug? I'll be happy to take a stab at a PR if it is, and if nobody else wants to handle it. I'm also suffering from the |
I've stopped using django-storages and moved to this for django-s3 stuff- lighter weight, and seems to be better supported. Also, it doesn't require that workaround :) https://github.com/etianen/django-s3-storage switching was easy even with existing data. |
@kyleknap when you get a chance, could you chime in on whether calling |
Ran into this issue recently while attempting to generate images, upload them to s3 for backup, and then attach them to an email. |
I'm using the following workaround:
|
@mgrdcm I think it is a bug. The control should be relinquished to the end user instead of closing it prematurely. |
@leogzyl A near perfect workaround except
Any solutions other than |
@zachliu I added an init method with a cast to work around the mypy issue:
|
3 years passed... Can it be either fixed or declared to be not a bug, so other libraries should build workarounds for it? |
I mean, it's clearly a bug, right? You can't specify that a method maybe, sometimes, occasionally closes your files. |
We're affected by this too. The NonCloseableBufferedReader workaround seems to do the trick, but hopefully we can remove it once this is fixed. |
The issue still appears on March 2020, @leogzyl 's workarounds solves the issue |
There is many of us with this issue, something should be done with it. |
I got this same issue on April 2021 using boto3 and upload_fileobj() function. |
This behavior is not documented. This behavior is not expected. The first version of boto did not implement this behavior. I've been using Python for over 20 years and have never seen a library close a file in this context. There is no reason why this function should close the file. The only thing more ridiculous than this bug existing, is that it has not been fixed in 4.5 years. If keeping the file open causes an issue, then the library has serious underlying problems. Aside from the numerous issues this creates with on-disk tempfiles (as one other user mentioned above) which can be nearly impossible to re-open, file-like objects in Python routinely include SpooledTemporaryFile and StringIO objects. This absurd, unexpected and undocumented behavior completely destroys these file-like objects and renders them unrecoverable. |
Suspected regression commit: 2f3d12c Diff: 2f3d12c#diff-fb5195c2216d4f5a452b31d518beae30b39667b0655d8a8fd1663a183575aa0aL167-R165 - client.put_object(Bucket=bucket, Key=key, Body=body, **extra_args)
+ with osutil.open_file_chunk_reader(
+ fileobj, 0, size, progress_callbacks) as body:
+ client.put_object(Bucket=bucket, Key=key, Body=body, **extra_args)
It appears that Put another way, |
boto3 closes file objects, see: boto/s3transfer#80 Add this to our mock for `upload_fileobj()`, too.
5 years anniversary of this issue and it's still open! I just wasted a couple of hours figuring out my problem and it came to this bug! Why isn't anyone working on this? |
It's tradition now. |
Tradition is continue :( |
Same here |
@saranglakare Almost 7 years and still counting 😂, just run into this issue today. |
I encountered this bug on May 3, 2024. |
Kinda disappointing this issue is almost 8 years old and not even a single mention in the documentation was made. Here's another hacky workaround: def patched_close():
print("good try boto3")
file = BytesIO()
# Save the original close function
original_close = file.close
# Patch the close function with ours
file.close = patched_close
client.upload_fileobj(Fileobj=file, Bucket=bucket, Key=name)
# Unpatch it
file.close = original_close |
Source boto/boto3#929
This is because we wrap them with the
InterruptReader
which closes them on exit.The text was updated successfully, but these errors were encountered: