Search code examples
pythonpython-3.9

Python 3.9: OSError: [Errno 9] Bad file descriptor during Temporary File


I am trying to retrieve an image from S3 and do some processing with it:

s3 = image_aws_session().client("s3")
with tempfile.TemporaryFile() as tmp:
    with open(tmp.name, "r+b") as f:
        s3.download_fileobj(
            Bucket=settings.S3_IMAGE_BUCKET,
            Key=s3_key,
            Fileobj=f,
        )
        f.seek(0)
        image_str = f.read().hex()
        print(image_str)
return image_str

I get a file back, and it also prints a nice long hash, so the fetching itself works. I had to obstruct the s3 key, since its not important.

However, then it errors out with OSError: [Errno 9] Bad file descriptor before returning the image hash. I have tried indenting back and forth to no avail. Maybe I am just not understanding something correctly


Solution

    1. You're opening the file in read binary mode, but download_fileobj attempts to write to it, that wouldn't work. Also, you're appending to it by including + which probably isn't necessary. Try open('wb')
    2. It's possible that the file isn't updated after download_fileobj completes. Try letting it close after the download and reopening it
    s3 = image_aws_session().client("s3")
    with tempfile.TemporaryFile() as tmp:
        with open(tmp.name, "wb") as f:
            s3.download_fileobj(
                Bucket=settings.S3_IMAGE_BUCKET,
                Key=s3_key,
                Fileobj=f,
            )
        with open(tmp.name, 'rb') as f:
            image_str = f.read().hex()
            print(image_str)
    
    return image_str