在boto3中获取当前用户帐户ID [英] getting the current user account-id in boto3

查看:109
本文介绍了在boto3中获取当前用户帐户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取帐户ID boto3 脚本中的当前用户".到目前为止,我最好的解决方案是解析当前用户学习:

>>> import boto3
>>> account_id = boto3.resource('iam').CurrentUser().arn.split(':')[4]

但是我想知道是否还有更轻量级"的方法.实际上

but I was wondering if there is a more 'lightweight' approach. In fact

>>> timeit.timeit("boto3.resource('iam').CurrentUser().arn",
... 'import boto3', number=10)
4.8895583080002325

,实际上我不需要 CurrentUser 我脚本中的资源.

and I actually do not need the CurrentUser resource in my script.

推荐答案

您可以使用STS API获取帐户ID:

You can get the account ID by using the STS API:

>>> import boto3
>>> boto3.client('sts').get_caller_identity().get('Account')
'012345678901'

这篇关于在boto3中获取当前用户帐户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆