Alasdair Alasdair - 1 year ago 351
Python Question

Equivalent to get_contents_to_file in boto3

In boto3, is there an equivalent of

, that copies the contents of an object to a file handle?

In boto, if I have an S3 object
, I can copy the contents to a temporary file with:

from tempfile import TemporaryFile
key = code_that_gets_key()

with TemporaryFile() as tmp_file:
key.get_contents_to_file(key, tmpfile)

I haven't found an equivalent in boto3.

I have been able to replace usage of
. However, that covers the case where I provide the filename. In this case, I want to provide the file handle as the argument.

Currently, I can get the code to work in boto3 by iterating over the body as follows:

with TemporaryFile() as tmp_file:
body = key.get()['Body']
for chunk in iter(lambda:, b''):

Is there a better way to do this in boto3?

Answer Source

As of V1.4.0 there is a download_fileobj function that does exactly what you want. As per the formal documentation:

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')

with open('filename', 'wb') as data:
    bucket.download_fileobj('mykey', data)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download