将 ListUserPolicies 与 AWS SDK 或 CLI 配合使用 - AWS Identity and Access Management

ListUserPolicies 与 AWS SDK 或 CLI 配合使用

以下代码示例演示如何使用 ListUserPolicies

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 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import ( "context" "encoding/json" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/iam" "github.com/aws/aws-sdk-go-v2/service/iam/types" "github.com/aws/smithy-go" ) // 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(ctx context.Context, userName string) ([]string, error) { var policies []string result, err := wrapper.IamClient.ListUserPolicies(ctx, &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 详细信息,请参阅《适用于 Go 的 AWS SDK API 参考》中的 ListUserPolicies

PowerShell
Tools for PowerShell V4

示例 1:此示例检索嵌入在名为 David 的 IAM 用户中的内联策略名称列表。

Get-IAMUserPolicyList -UserName David

输出

Davids_IAM_Admin_Policy
  • 有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 ListUserPolicies

Tools for PowerShell V5

示例 1:此示例检索嵌入在名为 David 的 IAM 用户中的内联策略名称列表。

Get-IAMUserPolicyList -UserName David

输出

Davids_IAM_Admin_Policy
  • 有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet 参考 (V5)》中的 ListUserPolicies

有关 AWS SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 AWS SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。