使用 Amazon 开发工具包列出 IAM 角色
以下代码示例显示如何列出 IAM 角色。
- .NET
-
- Amazon SDK for .NET
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 using System; using Amazon.IdentityManagement; using Amazon.IdentityManagement.Model; var client = new AmazonIdentityManagementServiceClient(); // Without the MaxItems value, the ListRolesAsync method will // return information for up to 100 roles. If there are more // than the MaxItems value or more than 100 roles, the response // value IsTruncated will be true. var request = new ListRolesRequest { MaxItems = 10, }; var response = new ListRolesResponse(); do { response = await client.ListRolesAsync(request); response.Roles.ForEach(role => { Console.WriteLine($"{role.RoleName} - ARN {role.Arn}"); }); // As long as response.IsTruncated is true, set request.Marker equal // to response.Marker and call ListRolesAsync again. if (response.IsTruncated) { request.Marker = response.Marker; } } while (response.IsTruncated);-
有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 ListRoles。
-
- Go
-
- SDK for Go V2
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 // ListRoles roles, err := service.ListRoles(context.Background(), &iam.ListRolesInput{}) if err != nil { panic("Could not list roles: " + err.Error()) } fmt.Println("☑️ list roles") for _, idxRole := range roles.Roles { fmt.Printf("%s\t%s\t%s\t", *idxRole.RoleId, *idxRole.RoleName, *idxRole.Arn) if idxRole.Description != nil { fmt.Print(*idxRole.Description) } fmt.Print("\n") }-
有关 API 详细信息,请参阅《Amazon SDK for Go API 参考》中的 ListRoles
。
-
- 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 { ListRolesCommand } from "@aws-sdk/client-iam"; // Set the parameters. const params = { Marker: 'MARKER', // This is a string value. MaxItems: 'MAX_ITEMS' // This is a number value. }; const run = async () => { try { const results = await iamClient.send(new ListRolesCommand(params)); console.log("Success", results); return results; } catch (err) { console.log("Error", err); } }; run();-
有关 API 详细信息,请参阅《Amazon SDK for JavaScript API 参考》中的 ListRoles。
-
- PHP
-
- SDK for PHP
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 $uuid = uniqid(); $service = new IamService(); /** * @param string $pathPrefix * @param string $marker * @param int $maxItems * @return Result * $roles = $service->listRoles(); */ public function listRoles($pathPrefix = "", $marker = "", $maxItems = 0) { $listRolesArguments = []; if ($pathPrefix) { $listRolesArguments["PathPrefix"] = $pathPrefix; } if ($marker) { $listRolesArguments["Marker"] = $marker; } if ($maxItems) { $listRolesArguments["MaxItems"] = $maxItems; } return $this->iamClient->listRoles($listRolesArguments); }-
有关 API 详细信息,请参阅《Amazon SDK for PHP API 参考》中的 ListRoles。
-
- Python
-
- 适用于 Python (Boto3) 的 SDK
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 def list_roles(count): """ Lists the specified number of roles for the account. :param count: The number of roles to list. """ try: roles = list(iam.roles.limit(count=count)) for role in roles: logger.info("Role: %s", role.name) except ClientError: logger.exception("Couldn't list roles for the account.") raise else: return roles-
有关 API 详细信息,请参阅《Amazon SDK for Python(Boto3)API 参考》中的 ListRoles。
-
- Ruby
-
- SDK for Ruby
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 # Lists up to a specified number of roles for the account. # # @param count [Integer] The maximum number of roles to list. # @return [Array] The names of the listed roles. def list_roles(count) role_names = [] @iam_resource.roles.limit(count).each_with_index do |role, index| puts("\t#{index + 1}: #{role.name}") role_names.append(role.name) end rescue Aws::Errors::ServiceError => e puts("Couldn't list roles for the account. Here's why:") puts("\t#{e.code}: #{e.message}") raise else role_names end-
有关 API 详细信息,请参阅《Amazon SDK for Ruby API 参考》中的 ListRoles。
-
- Rust
-
- SDK for Rust
-
注意 本文档适用于预览版中的软件开发工具包。软件开发工具包可能随时发生变化,不应在生产环境中使用。
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 pub async fn list_roles( client: &iamClient, path_prefix: Option<String>, marker: Option<String>, max_items: Option<i32>, ) -> Result<ListRolesOutput, SdkError<ListRolesError>> { let response = client .list_roles() .set_path_prefix(path_prefix) .set_marker(marker) .set_max_items(max_items) .send() .await?; Ok(response) }-
有关 API 详细信息,请参阅《Amazon SDK for Rust API 参考》中的 ListRoles
。
-
有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 IAM 与 Amazon 开发工具包配合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。