我使用以下命令成功地将图像文件从我的 S3 存储桶下载到我的本地计算机:
import os
import boto3
import botocore
files = ['images/dog_picture.png']
bucket = 'animals'
s3 = boto3.resource('s3')
for file in files:
s3.Bucket(bucket).download_file(file, os.path.basename(file))
但是,当我尝试指定本地计算机上应保存图像的目录时 as is done in the docs :
s3.Bucket(bucket).download_file(file, os.path.basename(file), '/home/user/storage/new_image.png')
我得到:
ValueError: Invalid extra_args key '/home/user/storage/new_image.png', must be one of: VersionId, SSECustomerAlgorithm, SSECustomerKey, SSECustomerKeyMD5, RequestPayer
我一定做错了什么,但我正在遵循文档中的示例。有人可以帮我指定一个本地目录吗?
最佳答案
查看文档,您提供了一个额外的参数
import boto3
s3 = boto3.resource('s3')
s3.Bucket('mybucket').download_file('hello.txt', '/tmp/hello.txt')
从文档中,hello.txt 是存储桶上对象的名称,/tmp/hello.txt 是您设备上的路径,因此正确的方法是
s3.Bucket(bucket).download_file(file, '/home/user/storage/new_image.png')
关于python - 使用 boto3 从 S3 存储桶下载文件时出现 ValueError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60114952/