如何在 Python 中使用 boto3 将 Wasabi/S3 对象下载为字符串/字节

你可以使用 io.BytesIO 将 S3 对象的内容存储在内存中,然后将其转换为 bytes,再解码为 str。以下示例将 myfile.txt 下载到内存中:

example-2.py
buf = io.BytesIO()
my_bucket.download_fileobj("myfile.txt", buf)
# 获取文件内容为字节
filecontent_bytes = buf.getvalue()
# ... 或转换为字符串
filecontent_str = buf.getvalue().decode("utf-8")

完整示例

example-1.py
import io

# 创建到 Wasabi / S3 的连接
s3 = boto3.resource('s3',
    endpoint_url = 'https://s3.eu-central-1.wasabisys.com',
    aws_access_key_id = 'MY_ACCESS_KEY',
    aws_secret_access_key = 'MY_SECRET_KEY'
)

# 获取 bucket 对象
my_bucket = s3.Bucket('boto-test')

# 下载到文件
buf = io.BytesIO()
my_bucket.download_fileobj("myfile.txt", buf)
# 获取文件内容为字节
filecontent_bytes = buf.getvalue()
# ... 或转换为字符串
filecontent_str = buf.getvalue().decode("utf-8")

print(filecontent_str)

Don’t forget to fill in MY_ACCESS_KEY and MY_SECRET_KEY. Depending on what region and what S3-compatible service you use, you might need to use another endpoint URL instead of https://s3.eu-central-1.wasabisys.com.


Check out similar posts by category: Python, S3