搭ListRolePolicies配 AWS SDK或使用 CLI - AWS SDK 程式碼範例

AWS 文檔 AWS SDK示例 GitHub 回購中有更多SDK示例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

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
SDK對於轉到 V2
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// 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(roleName string) ([]string, error) { var policies []string result, err := wrapper.IamClient.ListRolePolicies(context.TODO(), &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細資訊,請參閱AWS SDK for Go API參考ListRolePolicies中的。

JavaScript
SDK對於 JavaScript (3)
注意

還有更多關於 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 指令程ListRolePolicies式參考中的。

Python
SDK對於 Python(肉毒桿菌 3)
注意

還有更多關於 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細資訊,請參閱ListRolePoliciesAWS SDK的〈〉以取得 Python (Boto3) API 參考資料。

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
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細資訊,請參閱ListRolePoliciesAWS SDK的以取得 Rust API 參考

Swift
SDK為斯威夫特
注意

這是預覽版的售前版說明文件。SDK內容可能變動。

注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

public func listRolePolicies(role: String) async throws -> [String] { var policyList: [String] = [] var marker: String? = nil var isTruncated: Bool repeat { let input = ListRolePoliciesInput( marker: marker, roleName: role ) let output = try await client.listRolePolicies(input: input) guard let policies = output.policyNames else { return policyList } for policy in policies { policyList.append(policy) } marker = output.marker isTruncated = output.isTruncated } while isTruncated == true return policyList }
  • 有API關詳細信息,請參閱ListRolePoliciesAWS SDK的以獲取 Swift API 參考