你能从boto获得AWS账户名吗?

时间:2014-09-03 14:49:21

标签: python python-2.7 amazon-web-services boto3 boto

我有一个AWS密钥和密钥,并希望致电boto获取帐户名称。

我可以获得帐户ID,但AWS帐户名称是个谜。

4 个答案:

答案 0 :(得分:3)

来自Get AWS Account ID from Boto

id = boto3.client('sts').get_caller_identity().get('Account')

然后

name =   boto3.client('organizations').describe_account(AccountId=id).get('Account').get('Name')

答案 1 :(得分:3)

要在boto3中获取AWS账户别名:

alias = boto3.client('iam').list_account_aliases()['AccountAliases'][0]

要获取帐户ID(数字):

id =  boto3.client('sts').get_caller_identity().get('Account')

答案 2 :(得分:2)

只有您使用IAM并且想要检索该别名才有可能。如果您拥有root凭据,则无法检索帐户名称。

相关电话是:get_account_alias()

http://boto.readthedocs.org/en/latest/ref/iam.html#boto.iam.connection.IAMConnection.get_account_alias

答案 3 :(得分:0)

时间晚了,但可能对将来有所帮助。 如果您使用的是组织服务,则可以使用以下代码获取帐户名称

org = boto3.client('organizations')
account_name = org.describe_account(AccountId='account-id').get('Account')
print(account_name ['Name'])

For more info

相关问题