我正在创建一个IAM组,并试图打印创建的组名。当我尝试的时候,它给了我这个错误
KeyError: 'GroupName'这是我的功能
def cf_admin_iam_group():
iam = boto3.client('iam')
try:
response = iam.create_group(GroupName='Test')
print(response['GroupName'])
except botocore.exceptions.ClientError as error:
print(error)当我试图仅仅是print(response)
我得到了预期的输出
{'Group': {'Path': '/', 'GroupName': 'Test', 'GroupId': 'AGPAXVCO7KXYHZP24FQFZ', 'Arn':
'arn:aws:iam::526299125232:group/Test', 'CreateDate': datetime.datetime(2022, 9, 13, 19, 17, 51, tzinfo=tzutc())},
'ResponseMetadata': {'RequestId': '7b2e7c6c-a811-497a-b2c5-177c70a0464c',
'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '7b2e7c6c-a811-497a-b2c5-177c70a0464c',
'content-type': 'text/xml', 'content-length': '490', 'date': 'Tue, 13 Sep 2022 19:17:51 GMT'}, 'RetryAttempts': 0}}我不知道为什么运行print(response['GroupName'])会给我一个错误,而不是打印组名。
发布于 2022-09-13 19:43:20
response是一个字典,而GroupName是Group值的一个键,因此您需要使用:
print(response['Group']['GroupName'])https://stackoverflow.com/questions/73708352
复制相似问题