文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 AWS 开发工具包列出用户的内联 IAM policy
以下代码示例演示了如何列出用户的内联 IAM policy。
警告
为了避免安全风险,在开发专用软件或处理真实数据时,请勿使用 IAM 用户进行身份验证,而是使用与身份提供商的联合身份验证,例如 AWS IAM Identity Center。
- CLI
-
- AWS CLI
-
要列出 IAM 用户的策略
以下
list-user-policies
命令将列出附加到名为Bob
的 IAM 用户的策略。aws iam list-user-policies \ --user-name Bob
输出:
{ "PolicyNames": [ "ExamplePolicy", "TestPolicy" ] }
有关更多信息,请参阅《AWS IAM 用户指南》中的在 AWS 账户中创建 IAM 用户。
-
有关 API 的详细信息,请参阅AWS CLI命令参考ListUserPolicies
中的。
-
- Go
-
- 适用于 Go V2 的 SDK
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 // UserWrapper encapsulates user actions used in the examples. // It contains an IAM service client that is used to perform user actions. type UserWrapper struct { IamClient *iam.Client } // ListUserPolicies lists the inline policies for the specified user. func (wrapper UserWrapper) ListUserPolicies(userName string) ([]string, error) { var policies []string result, err := wrapper.IamClient.ListUserPolicies(context.TODO(), &iam.ListUserPoliciesInput{ UserName: aws.String(userName), }) if err != nil { log.Printf("Couldn't list policies for user %v. Here's why: %v\n", userName, err) } else { policies = result.PolicyNames } return policies, err }
-
有关 API 的详细信息,请参阅 AWS SDK for GoAPI 参考ListUserPolicies
中的。
-
列出角色的内联策略
列出策略