你好,
我正在尝试从非常大的 S3 存储桶下载所有文件。我像这样连接到 S3:
client = boto3.client('s3',
aws_access_key_id=tempCredentials.credentials.access_key,
aws_secret_access_key = tempCredentials.credentials.secret_key,
aws_session_token=tempCredentials.credentials.session_token)
由此,我做到:
# This is going to go through and fill in the dictionary with keys
from the buckets as specified above
paginator = client.get_paginator("list_objects")
page_iterator = paginator.paginate(Bucket=bucket["Name"])
l = 0
# We are going to have an list that will hold all the keys
key_list = []
for i in page_iterator:
c = i["Contents"]
for j in c:
key_list.append(j["Key"])
for j in key_list:
download(bucket["Name"], j, "/Users/ahussain/Desktop/S3_Scrubber/" + file_name_helper(j), client)
其中,我的下载功能为:
def download (bucket_name, key, path, client):
key_name = key
print("Dowloading %s..." % str(key))
client.download_file(bucket_name, key, path)
print("Download of %s complete!" % str(key))
return key_name
发生的情况是,我成功地遍历了存储桶并下载了大量 key ,但一段时间后程序停止下载 key 并给出了此错误:
botocore.exceptions.ClientError: An error occurred (400) when calling the HeadObject operation: Bad Request
我的猜测是,由于我使用 MFA 访问此 S3,所以我的 session 已过期,但我不确定。以前有人遇到过这个错误吗?
最佳答案
上面的答案要么是错误的,要么是过时的。我不知道。您可以延长凭证的有效期。它们可持续长达 12 小时。最长不超过 1 小时。转至 IAM > 角色 > 您指定的角色 > 编辑 session 持续时间。
根据 IAM documentation ,最大值由最大 CLI/API session 持续时间定义,最大值为 12 小时。
考虑到您的操作可能需要 1 小时以上且少于 12 小时,编辑可能会解决您的问题。如果超过 12 小时,请考虑编辑脚本以刷新凭据。老实说,我不确定如何做到这一点或者是否可能,但是这个 SO answer可能有帮助,以及 docs .
关于Python Boto3 : Error when trying to download files from AWS S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45468799/