ListRolePolicies与 AWS SDK 或 CLI 配合使用 - AWS SDK 代码示例

文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例

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

ListRolePolicies与 AWS SDK 或 CLI 配合使用

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

.NET
AWS SDK for .NET
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

/// <summary> /// List IAM role policies. /// </summary> /// <param name="roleName">The IAM role for which to list IAM policies.</param> /// <returns>A list of IAM policy names.</returns> public async Task<List<string>> ListRolePoliciesAsync(string roleName) { var listRolePoliciesPaginator = _IAMService.Paginators.ListRolePolicies(new ListRolePoliciesRequest { RoleName = roleName }); var policyNames = new List<string>(); await foreach (var response in listRolePoliciesPaginator.Responses) { policyNames.AddRange(response.PolicyNames); } return policyNames; }
  • 有关 API 的详细信息,请参阅 AWS SDK for .NET API 参考ListRolePolicies中的。

CLI
AWS CLI

列出附加到 IAM 角色的策略

以下 list-role-policies 命令将列出指定 IAM 角色的权限策略名称。

aws iam list-role-policies \ --role-name Test-Role

输出:

{ "PolicyNames": [ "ExamplePolicy" ] }

要查看附加到角色的信任策略,请使用 get-role 命令。要查看权限策略的详细信息,请使用 get-role-policy 命令。

有关更多信息,请参阅《AWS IAM 用户指南》中的创建 IAM 角色

  • 有关 API 的详细信息,请参阅AWS CLI 命令参考ListRolePolicies中的。

Go
适用于 Go V2 的 SDK
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

import ( "context" "encoding/json" "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" ) // RoleWrapper encapsulates AWS Identity and Access Management (IAM) role actions // used in the examples. // It contains an IAM service client that is used to perform role actions. type RoleWrapper struct { IamClient *iam.Client } // ListRolePolicies lists the inline policies for a role. func (wrapper RoleWrapper) ListRolePolicies(ctx context.Context, roleName string) ([]string, error) { var policies []string result, err := wrapper.IamClient.ListRolePolicies(ctx, &iam.ListRolePoliciesInput{ RoleName: aws.String(roleName), }) if err != nil { log.Printf("Couldn't list policies for role %v. Here's why: %v\n", roleName, err) } else { policies = result.PolicyNames } return policies, err }
  • 有关 API 的详细信息,请参阅 适用于 Go 的 AWS SDK API 参考ListRolePolicies中的。

JavaScript
适用于 JavaScript (v3) 的软件开发工具包
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

列出策略。

import { ListRolePoliciesCommand, IAMClient } from "@aws-sdk/client-iam"; const client = new IAMClient({}); /** * A generator function that handles paginated results. * The AWS SDK for JavaScript (v3) provides {@link https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/index.html#paginators | paginator} functions to simplify this. * * @param {string} roleName */ export async function* listRolePolicies(roleName) { const command = new ListRolePoliciesCommand({ RoleName: roleName, MaxItems: 10, }); let response = await client.send(command); while (response.PolicyNames?.length) { for (const policyName of response.PolicyNames) { yield policyName; } if (response.IsTruncated) { response = await client.send( new ListRolePoliciesCommand({ RoleName: roleName, MaxItems: 10, Marker: response.Marker, }), ); } else { break; } } }
  • 有关 API 的详细信息,请参阅 AWS SDK for JavaScript API 参考ListRolePolicies中的。

PHP
适用于 PHP 的 SDK
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

$uuid = uniqid(); $service = new IAMService(); public function listRolePolicies($roleName, $marker = "", $maxItems = 0) { $listRolePoliciesArguments = ['RoleName' => $roleName]; if ($marker) { $listRolePoliciesArguments['Marker'] = $marker; } if ($maxItems) { $listRolePoliciesArguments['MaxItems'] = $maxItems; } return $this->customWaiter(function () use ($listRolePoliciesArguments) { return $this->iamClient->listRolePolicies($listRolePoliciesArguments); }); }
  • 有关 API 的详细信息,请参阅 AWS SDK for PHP API 参考ListRolePolicies中的。

PowerShell
用于 PowerShell

示例 1:此示例返回 IAM 角色 lamda_exec_role 中嵌入的内联策略名称列表。要查看内联策略的详细信息,请使用 Get-IAMRolePolicy 命令。

Get-IAMRolePolicyList -RoleName lambda_exec_role

输出

oneClick_lambda_exec_role_policy
  • 有关 API 的详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考ListRolePolicies中的。

Python
适用于 Python 的 SDK(Boto3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

def list_policies(role_name): """ Lists inline policies for a role. :param role_name: The name of the role to query. """ try: role = iam.Role(role_name) for policy in role.policies.all(): logger.info("Got inline policy %s.", policy.name) except ClientError: logger.exception("Couldn't list inline policies for %s.", role_name) raise
  • 有关 API 的详细信息,请参阅适用ListRolePoliciesPython 的AWS SDK (Boto3) API 参考

Ruby
适用于 Ruby 的 SDK
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

# Lists policy ARNs attached to a role # # @param role_name [String] The name of the role # @return [Array<String>] List of policy ARNs def list_attached_policy_arns(role_name) response = @iam_client.list_attached_role_policies(role_name: role_name) response.attached_policies.map(&:policy_arn) rescue Aws::IAM::Errors::ServiceError => e @logger.error("Error listing policies attached to role: #{e.message}") [] end
  • 有关 API 的详细信息,请参阅 AWS SDK for Ruby API 参考ListRolePolicies中的。

Rust
适用于 Rust 的 SDK
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

pub async fn list_role_policies( client: &iamClient, role_name: &str, marker: Option<String>, max_items: Option<i32>, ) -> Result<ListRolePoliciesOutput, SdkError<ListRolePoliciesError>> { let response = client .list_role_policies() .role_name(role_name) .set_marker(marker) .set_max_items(max_items) .send() .await?; Ok(response) }
  • 有关 API 的详细信息,请参阅适用ListRolePoliciesRust 的AWS SDK API 参考

Swift
适用于 Swift 的 SDK
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

import AWSIAM import AWSS3 public func listRolePolicies(role: String) async throws -> [String] { var policyList: [String] = [] // Use "Paginated" to get all the role policies. // This lets the SDK handle the 'isTruncated' in "ListRolePoliciesOutput". let input = ListRolePoliciesInput( roleName: role ) let pages = client.listRolePoliciesPaginated(input: input) do { for try await page in pages { guard let policies = page.policyNames else { print("Error: no role policies returned.") continue } for policy in policies { policyList.append(policy) } } } catch { print("ERROR: listRolePolicies:", dump(error)) throw error } return policyList }
  • 有关 API 的详细信息,请参阅适用于 S wift 的AWS SDK API 参考ListRolePolicies中。