gpt4 book ai didi

python - 从 S3 存储桶下载图像并将其存储在本地以外的变量中 (boto3)

转载 作者:行者123 更新时间:2023-12-02 18:39:40 25 4
gpt4 key购买 nike

我想从存储桶 S3 下载图像并将其存储在变量中,而不是存储在本地电脑中,我该怎么做?

我使用此代码存储在本地:

BUCKET_NAME = 'mybucket' 
KEYFILE = 'myImage.jpg'

try:
s3.Bucket(BUCKET_NAME).download_file(KEYFILE, 'myImageInLocal.jpg')
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print("The object does not exist.")
else:
raise

最佳答案

您可以下载到BytesIO变量为 download_fileobj :

from io import BytesIO

BUCKET_NAME = 'mybucket'
KEYFILE = 'myImage.jpg'

s3_file = BytesIO()

try:
s3.Bucket(BUCKET_NAME).download_fileobj('myImageInLocal.jpg', s3_file)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print("The object does not exist.")
else:
raise

关于python - 从 S3 存储桶下载图像并将其存储在本地以外的变量中 (boto3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68191929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com