python - S3.Client.upload_file() 和 S3.Client.upload_fileobj() 有什么区别?

标签 python python-3.x amazon-web-services amazon-s3 boto3

根据 S3.Client.upload_fileS3.Client.upload_fileobj , upload_fileobj 听起来可能更快。但是有没有人知 Prop 体情况?我应该只上传文件,还是应该以二进制模式打开文件以使用 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 更好?有区别吗?

最佳答案

upload_fileobj 的要点是文件对象不必首先存储在本地磁盘上,而是可以在 RAM 中表示为文件对象。

Python 有 standard library module为此目的。

代码看起来像

import io
import boto3

s3 = boto3.client('s3')

fo = io.BytesIO(b'my data stored as file object in RAM')
s3.upload_fileobj(fo, 'mybucket', 'hello.txt')

在这种情况下,它会执行得更快,因为您不必从本地磁盘读取数据。

关于python - S3.Client.upload_file() 和 S3.Client.upload_fileobj() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52336902/

相关文章:

python - 使用 sklearn 聚类单变量时间序列

python-3.x - 如果 Feather 文件格式仍然相关,或者社区是否倾向于使用其他文件格式来存储大文件?

python - 遍历每列中的值

javascript - 对 API 网关的 XMLHttpRequest 不起作用

amazon-web-services - 将证书文件提供到 k8s 中的 pod 中

ubuntu - cron 错误错误分钟 + crontab 文件中的错误无法在 ubuntu 中安装

python - 如何在pycharm上安装解释器?

python - RAR 存档和 Gzip : Is there anything that I am missing? 中的巨大压缩差异

python - UserString 子类, 'str' 对象不可调用

python - 如何以编程方式设置交互参数?