See update at bottom - question slightly changed
I'm trying to download a file from s3 to a file-like object using boto3's .download_fileobj
method, however when I try to inspect the downloaded bytestream, it's empty. I'm not sure what I'm doing wrong however:
client = boto3.client('s3')
data = io.BytesIO()
client.download_fileobj(Bucket='mybucket', Key='myfile.wav', Fileobj=data)
print(data.read())
Which yields an empty bytestring:
b''
UPDATE :
kINDA SOLVED. So it turns out that adding data.seek(0)
after the download_fileobj
line solves the problem. In light of this, I am now looking for an answer that explains what this snippet does and why it fixes the problem.
CodePudding user response:
Thank you for this, I was running into the same problem. :o)
The reason this works is that file buffer objects work with an internal pointer to the current spot to read from or write to.
This is important when you pass the read()
method a number of bytes to read, or to continually write to the next section of the file.
As the client.download_fileobj()
writes to the byte stream, or any operation writes to any stream, the pointer is positioned to the end of the last write.
So you need to tell the file buffer object that you want to read from the beginning of what was just written.