使用 Amazon 开发工具包列出 IAM 组
以下代码示例显示如何列出 IAM 组。
- .NET
-
- Amazon SDK for .NET
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 using System; using Amazon.IdentityManagement; using Amazon.IdentityManagement.Model; var client = new AmazonIdentityManagementServiceClient(); var request = new ListGroupsRequest { MaxItems = 10, }; var response = await client.ListGroupsAsync(request); do { response.Groups.ForEach(group => { Console.WriteLine($"{group.GroupName} created on: {group.CreateDate}"); }); if (response.IsTruncated) { request.Marker = response.Marker; response = await client.ListGroupsAsync(request); } } while (response.IsTruncated);-
有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 ListGroups。
-
- Go
-
- SDK for Go V2
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 // ListGroups listGroupsResult, err := service.ListGroups(context.Background(), &iam.ListGroupsInput{}) if err != nil { panic("Couldn't list groups! " + err.Error()) } for _, group := range listGroupsResult.Groups { fmt.Printf("group %s - %s\n", *group.GroupId, *group.Arn) }-
有关 API 详细信息,请参阅《Amazon SDK for Go API 参考》中的 ListGroups
。
-
- 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 {ListGroupsCommand} from "@aws-sdk/client-iam"; // Set the parameters. export const params = { RoleName: 'ROLE_NAME', /* This is a number value. Required */ Marker: 'MARKER', /* This is a string value. Optional */ MaxItems: 'MAX_ITEMS' /* This is a number value. Optional */ }; export const run = async () => { try { const data = await iamClient.send(new ListGroupsCommand({})); console.log("Success", data.Groups); } catch (err) { console.log("Error", err); } } run();-
有关 API 详细信息,请参阅《Amazon SDK for JavaScript API 参考》中的 ListGroups。
-
- PHP
-
- SDK for PHP
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 $uuid = uniqid(); $service = new IamService(); public function listGroups($pathPrefix = "", $marker = "", $maxItems = 0) { $listGroupsArguments = []; if ($pathPrefix) { $listGroupsArguments["PathPrefix"] = $pathPrefix; } if ($marker) { $listGroupsArguments["Marker"] = $marker; } if ($maxItems) { $listGroupsArguments["MaxItems"] = $maxItems; } return $this->iamClient->listGroups($listGroupsArguments); }-
有关 API 详细信息,请参阅《Amazon SDK for PHP API 参考》中的 ListGroups。
-
- Python
-
- 适用于 Python (Boto3) 的 SDK
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 def list_groups(count): """ Lists the specified number of groups for the account. :param count: The number of groups to list. """ try: for group in iam.groups.limit(count): logger.info("Group: %s", group.name) except ClientError: logger.exception("Couldn't list groups for the account.") raise-
有关 API 详细信息,请参阅《Amazon SDK for Python(Boto3)API 参考》中的 ListGroups。
-
- Ruby
-
- SDK for Ruby
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 # Lists up to a specified number of groups for the account. # # @param count [Integer] The maximum number of groups to list. def list_groups(count) @iam_resource.groups.limit(count).each do |group| puts("\t#{group.name}") end rescue Aws::Errors::ServiceError => e puts("Couldn't list groups for the account. Here's why:") puts("\t#{e.code}: #{e.message}") raise end-
有关 API 详细信息,请参阅《Amazon SDK for Ruby API 参考》中的 ListGroups。
-
- Rust
-
- SDK for Rust
-
注意 本文档适用于预览版中的软件开发工具包。软件开发工具包可能随时发生变化,不应在生产环境中使用。
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 pub async fn list_groups( client: &iamClient, path_prefix: Option<String>, marker: Option<String>, max_items: Option<i32>, ) -> Result<ListGroupsOutput, SdkError<ListGroupsError>> { let response = client .list_groups() .set_path_prefix(path_prefix) .set_marker(marker) .set_max_items(max_items) .send() .await?; Ok(response) }-
有关 API 详细信息,请参阅《Amazon SDK for Rust API 参考》中的 ListGroups
。
-
有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 IAM 与 Amazon 开发工具包配合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。