Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to get_contents_to_file in boto3

In boto3, is there an equivalent of get_contents_to_file, that copies the contents of an object to a file handle?

In boto, if I have an S3 object key, 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 get_contents_to_filename with download_file. 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: body.read(4096), b''):
        filehandle.write(chunk)

Is there a better way to do this in boto3?

like image 913
Alasdair Avatar asked Feb 01 '16 17:02

Alasdair


People also ask

What is key in Boto?

wsdl" is the key.

How do I know if my S3 bucket is empty boto3?

Boto3 resource doesn't provide any method directly to check if the key exists in the S3 bucket. Hence, you can load the S3 object using the load() method. If there is no exception thrown, then the key exists. If there is a client error thrown and the error code is 404 , then the key doesn't exist in the bucket.


1 Answers

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')
obj = bucket.Object('mykey')

with open('filename', 'wb') as data:
    obj.download_fileobj(data)

The operation is also available on the bucket resource and s3 client as well, for example:

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

with open('filename', 'wb') as data:
    bucket.download_fileobj('mykey', data)
like image 130
Peter Brittain Avatar answered Sep 18 '22 12:09

Peter Brittain