S3.Client.upload_file()和S3.Client.upload_fileobj()有什么区别? [英] What is the difference between S3.Client.upload_file() and S3.Client.upload_fileobj()?
问题描述
According to S3.Client.upload_file and S3.Client.upload_fileobj, upload_fileobj
may sound faster. But does anyone know specifics? Should I just upload the file, or should I open the file in binary mode to use upload_fileobj
? In other words,
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版更好吗?有区别吗?
Is version 1 or version 2 better? Is there a difference?
推荐答案
upload_fileobj
的要点是文件对象不必首先存储在本地磁盘上,但可以表示为文件对象在RAM中.
The main point with upload_fileobj
is that file object doesn't have to be stored on local disk in the first place, but may be represented as file object in RAM.
Python为此具有标准库模块.
Python have standard library module for that purpose.
代码看起来像
import io
fo = io.BytesIO(b'my data stored as file object in RAM')
s3.upload_fileobj(fo, 'mybucket', 'hello.txt')
在这种情况下,由于您不必从本地磁盘读取数据,因此它的执行速度会更快.
In that case it will perform faster, since you don't have to read from local disk.
这篇关于S3.Client.upload_file()和S3.Client.upload_fileobj()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!