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
+
which probably isn't necessary. Try open('wb')
download_fileobj
completes. Try letting it close after the download and reopening its3 = 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