根据 S3.Client.upload_file 和 S3.Client.upload_fileobj , upload_fileobj
听起来可能更快。但是有没有人知道具体情况?我应该只上传文件,还是应该以二进制模式打开文件以使用 upload_fileobj
?换句话说,
import boto3
s3 = boto3.resource('s3')
### Version 1
s3.meta.client.upload_file('/tmp/hello.txt', 'mybucket', 'hello.txt')
### Version 2
with open('/tmp/hello.txt', 'rb') as data:
s3.upload_fileobj(data, 'mybucket', 'hello.txt')
版本 1 还是版本 2 更好?有区别吗?
原文由 Flair 发布,翻译遵循 CC BY-SA 4.0 许可协议
upload_fileobj
的要点是文件对象不必首先存储在本地磁盘上,但可以在 RAM 中表示为文件对象。Python 有用于此目的的 标准库模块。
代码看起来像
在这种情况下,它会执行得更快,因为您不必从本地磁盘读取。