且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 boto3 将 S3 对象保存到文件

更新时间:2022-11-07 08:16:23

Boto3 最近进行了一项定制,有助于解决此问题(除其他外).它目前暴露在低级 S3 客户端上,可以这样使用:

There is a customization that went into Boto3 recently which helps with this (among other things). It is currently exposed on the low-level S3 client, and can be used like this:

s3_client = boto3.client('s3')
open('hello.txt').write('Hello, world!')

# Upload the file to S3
s3_client.upload_file('hello.txt', 'MyBucket', 'hello-remote.txt')

# Download the file from S3
s3_client.download_file('MyBucket', 'hello-remote.txt', 'hello2.txt')
print(open('hello2.txt').read())

这些函数将自动处理读取/写入文件以及对大文件并行执行分段上传.

These functions will automatically handle reading/writing files as well as doing multipart uploads in parallel for large files.

请注意,s3_client.download_file 不会创建目录.它可以创建为 pathlib.Path('/path/to/file.txt').parent.mkdir(parents=True,exist_ok=True).

Note that s3_client.download_file won't create a directory. It can be created as pathlib.Path('/path/to/file.txt').parent.mkdir(parents=True, exist_ok=True).