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?
wsdl" is the key.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With