如何使用boto3 S3对象保存到一个文件 [英] How to save S3 object to a file using boto3
问题描述
我试图做一个Hello World新 boto3 客户端AWS。
I'm trying to do a "hello world" with new boto3 client for AWS.
用例我有相当简单:从S3获得的对象,并将其保存到文件
The use-case I have is fairly simple: get object from S3 and save it to the file.
在博托2.X我会做这样的:
In boto 2.X I would do it like this:
import boto
key = boto.connect_s3().get_bucket('foo').get_key('foo')
key.get_contents_to_filename('/tmp/foo')
在博托3。我找不到一个干净的方式做同样的事情,所以我手动遍历流的对象:
In boto 3 . I can't find a clean way to do the same thing, so I'm manually iterating over the "Streaming" object:
import boto3
key = boto3.resource('s3').Object('fooo', 'docker/my-image.tar.gz').get()
with open('/tmp/my-image.tar.gz', 'w') as f:
chunk = key['Body'].read(1024*8)
while chunk:
f.write(chunk)
chunk = key['Body'].read(1024*8)
或
import boto3
key = boto3.resource('s3').Object('fooo', 'docker/my-image.tar.gz').get()
with open('/tmp/my-image.tar.gz', 'w') as f:
for chunk in iter(lambda: key['Body'].read(4096), b''):
f.write(chunk)
和它工作正常。我想知道是否有任何原生boto3功能,将做同样的工作?
And it works fine. I was wondering is there any "native" boto3 function that will do the same task?
推荐答案
涡,
有一个定制最近走进宝途3,与此帮助(除其他事项外)。它是目前公开的低级别S3客户机上,并且可以用于这样的:
There is a customization that went into Boto 3 recently which helps with this (among other things). It is currently exposed on the low-level S3 client, and can be used like this:
s3_client = boto3.client('s3')
open('hello.txt').write('Hello, world!')
# Upload the file to S3
s3_client.upload_file('hello.txt', 'MyBucket', 'hello-remote.txt')
# Download the file from S3
s3_client.download_file('MyBucket', 'hello-remote.txt', 'hello2.txt')
print(open('hello2.txt').read())
这些功能将自动处理读/写文件,以及做多部分上传平行大文件。请让我知道,如果您还有其他的问题!
These functions will automatically handle reading/writing files as well as doing multipart uploads in parallel for large files. Please let me know if you have other questions!
这篇关于如何使用boto3 S3对象保存到一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!