根据 S3.Client.upload_file和 S3.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/