【发布时间】:2020-05-28 18:51:25
【问题描述】:
我正在尝试编写使用许多不同 AWS 密钥的 Python 代码,其中一些可能已过期。给定一个 AWS 密钥对作为字符串,我需要使用 boto3 检查给定的密钥对是否有效。我宁愿不必做任何事情,比如使用 os.system 来运行
echo "$aws_key_id
$aws_secret_key\n\n" | aws configure
然后读取aws list-buckets.的回复
答案应该类似于
def check_aws_validity(key_id, secret):
pass
其中key_id 和secret 是字符串。
请注意,这不是 Verifying S3 credentials w/o GET or PUT using boto3 的重复,因为我在 boto3.profile 中没有密钥。
提前致谢!
编辑 从 John Rotenstein 的回答中,我得到了以下功能。
def check_aws_validity(key_id, secret):
try:
client = boto3.client('s3', aws_access_key_id=key_id, aws_secret_access_key=secret)
response = client.list_buckets()
return true
except Exception as e:
if str(e)!="An error occurred (InvalidAccessKeyId) when calling the ListBuckets operation: The AWS Access Key Id you provided does not exist in our records.":
return true
return false
【问题讨论】:
-
到目前为止你写了什么?你解决问题的方法是什么?
-
答案就在我的问题中。
标签: python-3.x amazon-web-services boto3