使用 Amazon 开发工具包列出 IAM policy - Amazon Identity and Access Management
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

使用 Amazon 开发工具包列出 IAM policy

以下代码示例显示如何列出 IAM policy。

.NET
Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅 GitHub

using Amazon.IdentityManagement; using Amazon.IdentityManagement.Model; using System; var client = new AmazonIdentityManagementServiceClient(); var request = new ListPoliciesRequest { MaxItems = 10, }; var response = new ListPoliciesResponse(); do { response = await client.ListPoliciesAsync(request); response.Policies.ForEach(policy => { Console.Write($"{policy.PolicyName} "); Console.Write($"with ID: {policy.PolicyId} "); Console.Write($"and ARN: {policy.Arn}. "); Console.WriteLine($"It was created on {policy.CreateDate}."); }); if (response.IsTruncated) { request.Marker = response.Marker; } } while (response.IsTruncated);
  • 有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 ListPolicies

Go
SDK for Go V2
提示

要了解如何设置和运行此示例,请参阅 GitHub

// ListPolicies policyListResponse, err := service.ListPolicies(context.Background(), &iam.ListPoliciesInput{}) if err != nil { panic("Couldn't get list of policies! " + err.Error()) } fmt.Print("PolicyName\tARN") for _, policy := range policyListResponse.Policies { fmt.Printf("%s\t%s\n", *policy.PolicyName, *policy.Arn) }
  • 有关 API 详细信息,请参阅《Amazon SDK for Go API 参考》中的 ListPolicies

JavaScript
SDK for JavaScript V3
提示

要了解如何设置和运行此示例,请参阅 GitHub

创建客户端。

import { IAMClient } from "@aws-sdk/client-iam"; // Set the AWS Region. const REGION = "REGION"; // For example, "us-east-1". // Create an IAM service client object. const iamClient = new IAMClient({ region: REGION }); export { iamClient };

列出策略。

// Import required AWS SDK clients and commands for Node.js. import { iamClient } from "./libs/iamClient.js"; import {ListPoliciesCommand} from "@aws-sdk/client-iam"; // Set the parameters. export const params = { Marker: 'MARKER', MaxItems: 'MAX_ITEMS', OnlyAttached: "ONLY_ATTACHED", /* Options are "true" or "false"*/ PathPrefix: 'PATH_PREFIX', PolicyUsageFilter: "POLICY_USAGE_FILTER", /* Options are "PermissionsPolicy" or "PermissionsBoundary"*/ Scope: "SCOPE" /* Options are "All", "AWS", "Local"*/ }; export const run = async () => { try { const results = await iamClient.send(new ListPoliciesCommand(params)); console.log("Success", results); return results; } catch (err) { console.log("Error", err); } }; run();
  • 有关 API 详细信息,请参阅《Amazon SDK for JavaScript API 参考》中的 ListPolicies

PHP
SDK for PHP
提示

要了解如何设置和运行此示例,请参阅 GitHub

$uuid = uniqid(); $service = new IamService(); public function listPolicies($pathPrefix = "", $marker = "", $maxItems = 0) { $listPoliciesArguments = []; if ($pathPrefix) { $listPoliciesArguments["PathPrefix"] = $pathPrefix; } if ($marker) { $listPoliciesArguments["Marker"] = $marker; } if ($maxItems) { $listPoliciesArguments["MaxItems"] = $maxItems; } return $this->iamClient->listPolicies($listPoliciesArguments); }
  • 有关 API 详细信息,请参阅《Amazon SDK for PHP API 参考》中的 ListPolicies

Python
适用于 Python (Boto3) 的 SDK
提示

要了解如何设置和运行此示例,请参阅 GitHub

def list_policies(scope): """ Lists the policies in the current account. :param scope: Limits the kinds of policies that are returned. For example, 'Local' specifies that only locally managed policies are returned. :return: The list of policies. """ try: policies = list(iam.policies.filter(Scope=scope)) logger.info("Got %s policies in scope '%s'.", len(policies), scope) except ClientError: logger.exception("Couldn't get policies for scope '%s'.", scope) raise else: return policies
  • 有关 API 详细信息,请参阅《Amazon SDK for Python(Boto3)API 参考》中的 ListPolicies

Ruby
SDK for Ruby
提示

要了解如何设置和运行此示例,请参阅 GitHub

# Lists up to a specified number of policies in the account. # # @param count [Integer] The maximum number of policies to list. # @return [Array] The Amazon Resource Names (ARNs) of the policies listed. def list_policies(count) policy_arns = [] @iam_resource.policies.limit(count).each_with_index do |policy, index| puts("\t#{index + 1}: #{policy.policy_name}: #{policy.arn}") policy_arns.append(policy.arn) end rescue Aws::Errors::ServiceError => e puts("Couldn't list policies for the account. Here's why:") puts("\t#{e.code}: #{e.message}") raise else policy_arns end
  • 有关 API 详细信息,请参阅《Amazon SDK for Ruby API 参考》中的 ListPolicies

Rust
SDK for Rust
注意

本文档适用于预览版中的软件开发工具包。软件开发工具包可能随时发生变化,不应在生产环境中使用。

提示

要了解如何设置和运行此示例,请参阅 GitHub

pub async fn list_policies( client: &iamClient, path_prefix: Option<String>, marker: Option<String>, max_items: Option<i32>, ) -> Result<ListPoliciesOutput, SdkError<ListPoliciesError>> { let response = client .list_policies() .set_path_prefix(path_prefix) .set_marker(marker) .set_max_items(max_items) .send() .await?; Ok(response) }
  • 有关 API 详细信息,请参阅《Amazon SDK for Rust API 参考》中的 ListPolicies

有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 IAM 与 Amazon 开发工具包配合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。