python - 使用 boto3 从 S3 存储桶下载文件时出现 ValueError?

标签 python python-3.x amazon-web-services amazon-s3 boto3

我使用以下命令成功地将图像文件从我的 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/

相关文章:

python - 如何在 setup.py 中指定显式的 python 打包依赖项?

python - 无法在 WSL 中安装或升级 Python 3.10.8

python - 在python中并行化for循环

amazon-web-services - API 网关 : how to grant permissions based on the policy of the caller

amazon-web-services - AWS VSToolKit .Config 文件

Python,如何从列表列表中有效地制作嵌套字典

Python SQLAlchemy - "MySQL server has gone away"

python - 将面板数据 reshape 为数据框 pandas

amazon-web-services - AWS CLI - 将完整输出保存为日志文件

python - ansible-container 不是 JSON 可序列化的