在boto3中,是否有相当于 get_contents_to_file
,将对象的内容复制到文件句柄?
在boto中,如果我有一个S3对象 key
,我可以将内容复制到临时文件中:
from tempfile import TemporaryFile
key = code_that_gets_key()
with TemporaryFile() as tmp_file:
key.get_contents_to_file(key, tmpfile)
我还没有找到boto3中的等价物。
我已经能够替换使用了 get_contents_to_filename
同 download_file
。但是,这涵盖了我提供文件名的情况。在这种情况下,我想提供文件句柄作为参数。
目前,我可以通过迭代主体来获取代码在boto3中工作,如下所示:
with TemporaryFile() as tmp_file:
body = key.get()['Body']
for chunk in iter(lambda: body.read(4096), b''):
filehandle.write(chunk)
在boto3中有更好的方法吗?
作为 V1.4.0 有一个 download_fileobj
完全符合你想要的功能。根据正式文件:
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
obj = bucket.Object('mykey')
with open('filename', 'wb') as data:
obj.download_fileobj(data)
该操作也可在 桶资源 和 s3客户端 同样,例如:
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
with open('filename', 'wb') as data:
bucket.download_fileobj('mykey', data)
正确的答案是使用NamedTemporaryFile而不是TemporaryFile:
with NamedTemporaryFile() as tmp_file:
file_name = tmp_file.name # This is what you are looking for
更多文档: https://docs.python.org/2/library/tempfile.html
Peter的答案是正确的,但我想指出,目前大部分AWS都没有部署boto3 1.4,最值得注意的是AWS Lambda。
这并不妨碍您即时升级,但如果您在全新安装上运行代码,请务必检查
boto3.__version__ >= '1.4.0'
如果没有,请升级库。希望这将很快修复,这将没有实际意义。