使用 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/config" "github.com/aws/aws-sdk-go-v2/service/iam" ) // IAMUpdateUserAPI defines the interface for the UpdateUser function. // We use this interface to test the function using a mocked service. type IAMUpdateUserAPI interface { UpdateUser(ctx context.Context, params *iam.UpdateUserInput, optFns ...func(*iam.Options)) (*iam.UpdateUserOutput, error) } // RenameUser changes the name for an AWS Identity and Access Management (IAM) user. // 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 UpdateUserOutput object containing the result of the service call and nil. // Otherwise, nil and an error from the call to UpdateUser. func RenameUser(c context.Context, api IAMUpdateUserAPI, input *iam.UpdateUserInput) (*iam.UpdateUserOutput, error) { return api.UpdateUser(c, input) } func main() { userName := flag.String("u", "", "The name of the user") newName := flag.String("n", "", "The new name of the user") flag.Parse() if *userName == "" || *newName == "" { fmt.Println("You must supply a user name and new name (-u USERNAME -n NEW-NAME)") return } cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := iam.NewFromConfig(cfg) input := &iam.UpdateUserInput{ UserName: userName, NewUserName: newName, } _, err = RenameUser(context.TODO(), client, input) if err != nil { fmt.Println("Got an error updating user " + *userName) } fmt.Println("Updated user name from: " + *userName + " to: " + *newName) }
  • 有关 API 详细信息,请参阅《Amazon SDK for Go API 参考》中的 UpdateUser

Java
SDK for Java 2.x
提示

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

public static void updateIAMUser(IamClient iam, String curName,String newName ) { try { UpdateUserRequest request = UpdateUserRequest.builder() .userName(curName) .newUserName(newName) .build(); iam.updateUser(request); System.out.printf("Successfully updated user to username %s", newName); } catch (IamException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Java 2.x API 参考》中的 UpdateUser

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 { UpdateUserCommand } from "@aws-sdk/client-iam"; // Set the parameters. export const params = { UserName: "ORIGINAL_USER_NAME", //ORIGINAL_USER_NAME NewUserName: "NEW_USER_NAME", //NEW_USER_NAME }; export const run = async () => { try { const data = await iamClient.send(new UpdateUserCommand(params)); console.log("Success, username updated"); 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'}); var params = { UserName: process.argv[2], NewUserName: process.argv[3] }; iam.updateUser(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
Kotlin
SDK for Kotlin
注意

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

提示

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

suspend fun updateIAMUser(curName: String?, newName: String?) { val request = UpdateUserRequest { userName = curName newUserName = newName } IamClient { region = "AWS_GLOBAL" }.use { iamClient -> iamClient.updateUser(request) println("Successfully updated user to $newName") } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Kotlin API 参考》中的 UpdateUser

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

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

def update_user(user_name, new_user_name): """ Updates a user's name. :param user_name: The current name of the user to update. :param new_user_name: The new name to assign to the user. :return: The updated user. """ try: user = iam.User(user_name) user.update(NewUserName=new_user_name) logger.info("Renamed %s to %s.", user_name, new_user_name) except ClientError: logger.exception("Couldn't update name for user %s.", user_name) raise return user
  • 有关 API 详细信息,请参阅《Amazon SDK for Python(Boto3)API 参考》中的 UpdateUser

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