python - How to save S3 object to a file using boto3 -


i'm trying "hello world" new boto3 client aws.

the use-case have simple: object s3 , save file.

in boto 2.x this:

import boto key = boto.connect_s3().get_bucket('foo').get_key('foo') key.get_contents_to_filename('/tmp/foo') 

in boto 3 . can't find clean way same thing, i'm manually iterating on "streaming" object:

import boto3 key = boto3.resource('s3').object('fooo', 'docker/my-image.tar.gz').get() open('/tmp/my-image.tar.gz', 'w') f:     chunk = key['body'].read(1024*8)     while chunk:         f.write(chunk)         chunk = key['body'].read(1024*8) 

or

import boto3 key = boto3.resource('s3').object('fooo', 'docker/my-image.tar.gz').get() open('/tmp/my-image.tar.gz', 'w') f:     chunk in iter(lambda: key['body'].read(4096), b''):         f.write(chunk) 

and works fine. wondering there "native" boto3 function same task?

there customization went boto3 helps (among other things). exposed on low-level s3 client, , can used this:

s3_client = boto3.client('s3') open('hello.txt').write('hello, world!')  # upload file s3 s3_client.upload_file('hello.txt', 'mybucket', 'hello-remote.txt')  # download file s3 s3_client.download_file('mybucket', 'hello-remote.txt', 'hello2.txt') print(open('hello2.txt').read()) 

these functions automatically handle reading/writing files doing multipart uploads in parallel large files.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -