Equivalent to get_contents_to_file in boto3

The correct answer would be to use NamedTemporaryFile instead of TemporaryFile:

with NamedTemporaryFile() as tmp_file:
    file_name = tmp_file.name # This is what you are looking for

More docs here: https://docs.python.org/2/library/tempfile.html


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)