

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# `ListUserPolicies`与 AWS SDK 或 CLI 配合使用
<a name="iam_example_iam_ListUserPolicies_section"></a>

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

------
#### [ CLI ]

**AWS CLI**  
**列出 IAM 用户的策略**  
以下 `list-user-policies` 命令列出附加到名为 `Bob` 的 IAM 用户的策略。  

```
aws iam list-user-policies \
    --user-name Bob
```
输出：  

```
{
    "PolicyNames": [
        "ExamplePolicy",
        "TestPolicy"
    ]
}
```
有关更多信息，请参阅 [IAM 用户指南中的在您的 AWS 账户中创建AWS](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html) *IAM 用户*。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[ListUserPolicies](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iam/list-user-policies.html)*中的。

------
#### [ Go ]

**适用于 Go 的 SDK V2**  
 还有更多相关信息 GitHub。在 [AWS 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/gov2/iam#code-examples)中查找完整示例，了解如何进行设置和运行。

```
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](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/iam#Client.ListUserPolicies)*中的。

------
#### [ PowerShell ]

**适用于 PowerShell V4 的工具**  
**示例 1：此示例检索嵌入在名为 `David` 的 IAM 用户中的内联策略名称列表。**  

```
Get-IAMUserPolicyList -UserName David
```
**输出**：  

```
Davids_IAM_Admin_Policy
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 4) [ListUserPolicies](https://docs.aws.amazon.com/powershell/v4/reference)中的。

**适用于 PowerShell V5 的工具**  
**示例 1：此示例检索嵌入在名为 `David` 的 IAM 用户中的内联策略名称列表。**  

```
Get-IAMUserPolicyList -UserName David
```
**输出**：  

```
Davids_IAM_Admin_Policy
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [ListUserPolicies](https://docs.aws.amazon.com/powershell/v5/reference)中的。

------