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

使用 Amazon 开发工具包列出 IAM 账户别名

以下代码示例展示如何列出 IAM 账户别名。

Go
SDK for Go V2
提示

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

package main import ( "context" "flag" "fmt" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/iam" ) // IAMListAccountAliasesAPI defines the interface for the ListAccountAliases function. // We use this interface to test the function using a mocked service. type IAMListAccountAliasesAPI interface { ListAccountAliases(ctx context.Context, params *iam.ListAccountAliasesInput, optFns ...func(*iam.Options)) (*iam.ListAccountAliasesOutput, error) } // GetAccountAliases retrieves the aliases for your AWS Identity and Access Management (IAM) account. // Inputs: // c is the context of the method call, which includes the AWS Region. // api is the interface that defines the method call. // input defines the input arguments to the service call. // Output: // If successful, a ListAccountAliasesOutput object containing the result of the service call and nil. // Otherwise, nil and an error from the call to ListAccountAliases. func GetAccountAliases(c context.Context, api IAMListAccountAliasesAPI, input *iam.ListAccountAliasesInput) (*iam.ListAccountAliasesOutput, error) { return api.ListAccountAliases(c, input) } func main() { maxItems := flag.Int("m", 10, "Maximum number of aliases to list") flag.Parse() if *maxItems < 0 { *maxItems = 10 } cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := iam.NewFromConfig(cfg) input := &iam.ListAccountAliasesInput{ MaxItems: aws.Int32(int32(*maxItems)), } result, err := GetAccountAliases(context.TODO(), client, input) if err != nil { fmt.Println("Got an error retrieving account aliases") fmt.Println(err) return } for i, alias := range result.AccountAliases { fmt.Printf("Alias %d: %s\n", i, alias) } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Go API 参考》中的 ListAccountAliases

Java
SDK for Java 2.x
提示

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

public static void listAliases(IamClient iam) { try { ListAccountAliasesResponse response = iam.listAccountAliases(); for (String alias : response.accountAliases()) { System.out.printf("Retrieved account alias %s", alias); } } catch (IamException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Java 2.x API 参考》中的 ListAccountAliases

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 { ListAccountAliasesCommand } from "@aws-sdk/client-iam"; // Set the parameters. export const params = { MaxItems: 5 }; export const run = async () => { try { const data = await iamClient.send(new ListAccountAliasesCommand(params)); console.log("Success", data); return data; } catch (err) { console.log("Error", err); } }; run();
SDK for JavaScript V2
提示

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

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the IAM service object var iam = new AWS.IAM({apiVersion: '2010-05-08'}); iam.listAccountAliases({MaxItems: 10}, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
Kotlin
SDK for Kotlin
注意

这是适用于预览版中功能的预发行文档。本文档随时可能更改。

提示

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

suspend fun listAliases() { IamClient { region = "AWS_GLOBAL" }.use { iamClient -> val response = iamClient.listAccountAliases(ListAccountAliasesRequest {}) response.accountAliases?.forEach { alias -> println("Retrieved account alias $alias") } } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Kotlin API 参考》中的 ListAccountAliases

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

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

def list_aliases(): """ Gets the list of aliases for the current account. An account has at most one alias. :return: The list of aliases for the account. """ try: response = iam.meta.client.list_account_aliases() aliases = response['AccountAliases'] if len(aliases) > 0: logger.info("Got aliases for your account: %s.", ','.join(aliases)) else: logger.info("Got no aliases for your account.") except ClientError: logger.exception("Couldn't list aliases for your account.") raise else: return response['AccountAliases']
  • 有关 API 详细信息,请参阅《Amazon SDK for Python(Boto3)API 参考》中的 ListAccountAliases

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