本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
管理 IAM 访问密钥
创建访问密钥
要创建 IAM 访问密钥,请使用CreateAccessKeyRequest
IamClient’s
createAccessKey
方法。
注意
您必须将区域设置为 AWS_G LOBAL 才能使IamClient
呼叫生效,因为 IAM 这是一项全球服务。
导入
import software.amazon.awssdk.services.iam.model.CreateAccessKeyRequest; import software.amazon.awssdk.services.iam.model.CreateAccessKeyResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.iam.IamClient; import software.amazon.awssdk.services.iam.model.IamException;
代码
public static String createIAMAccessKey(IamClient iam,String user) { try { CreateAccessKeyRequest request = CreateAccessKeyRequest.builder() .userName(user).build(); CreateAccessKeyResponse response = iam.createAccessKey(request); String keyId = response.accessKey().accessKeyId(); return keyId; } catch (IamException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
请参阅上的完整示例
列出访问密钥
要列出给定用户的访问密钥,请创建一个包含要列出密钥的用户名的ListAccessKeysRequest
IamClient’s
listAccessKeys
方法。
注意
如果您不向提供用户名listAccessKeys
,它将尝试列出与签署请求的用户关联 Amazon Web Services 账户 的访问密钥。
导入
import software.amazon.awssdk.services.iam.model.AccessKeyMetadata; import software.amazon.awssdk.services.iam.model.IamException; import software.amazon.awssdk.services.iam.model.ListAccessKeysRequest; import software.amazon.awssdk.services.iam.model.ListAccessKeysResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.iam.IamClient;
代码
public static void listKeys( IamClient iam,String userName ){ try { boolean done = false; String newMarker = null; while (!done) { ListAccessKeysResponse response; if(newMarker == null) { ListAccessKeysRequest request = ListAccessKeysRequest.builder() .userName(userName).build(); response = iam.listAccessKeys(request); } else { ListAccessKeysRequest request = ListAccessKeysRequest.builder() .userName(userName) .marker(newMarker).build(); response = iam.listAccessKeys(request); } for (AccessKeyMetadata metadata : response.accessKeyMetadata()) { System.out.format("Retrieved access key %s", metadata.accessKeyId()); } if (!response.isTruncated()) { done = true; } else { newMarker = response.marker(); } } } catch (IamException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
listAccessKeys
的结果分页显示 (默认情况下,每个调用最多返回 100 个记录)。您可以调用返回isTruncated
的ListAccessKeysResponse
marker
中的 ListAccessKeysResponse
并在创建新请求时使用它。在下次调用 listAccessKeys
时使用该新请求。
请参阅上的完整示例
检索上次使用访问密钥的时间
要获取上次使用访问密钥的时间,请使用访问密钥的 ID 调用该IamClient’s
getAccessKeyLastUsed
方法(可以使用GetAccessKeyLastUsedRequest
然后,您可以使用返回的GetAccessKeyLastUsedResponse
导入
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.iam.IamClient; import software.amazon.awssdk.services.iam.model.GetAccessKeyLastUsedRequest; import software.amazon.awssdk.services.iam.model.GetAccessKeyLastUsedResponse; import software.amazon.awssdk.services.iam.model.IamException;
代码
public static void getAccessKeyLastUsed(IamClient iam, String accessId ){ try { GetAccessKeyLastUsedRequest request = GetAccessKeyLastUsedRequest.builder() .accessKeyId(accessId).build(); GetAccessKeyLastUsedResponse response = iam.getAccessKeyLastUsed(request); System.out.println("Access key was last used at: " + response.accessKeyLastUsed().lastUsedDate()); } catch (IamException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } System.out.println("Done"); }
请参阅上的完整示例
激活或停用访问密钥
您可以激活或停用访问密钥,方法是创建UpdateAccessKeyRequest
status
IamClient’s
updateAccessKey
方法。
导入
import software.amazon.awssdk.services.iam.model.IamException; import software.amazon.awssdk.services.iam.model.StatusType; import software.amazon.awssdk.services.iam.model.UpdateAccessKeyRequest; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.iam.IamClient;
代码
public static void updateKey(IamClient iam, String username, String accessId, String status ) { try { if (status.toLowerCase().equalsIgnoreCase("active")) { statusType = StatusType.ACTIVE; } else if (status.toLowerCase().equalsIgnoreCase("inactive")) { statusType = StatusType.INACTIVE; } else { statusType = StatusType.UNKNOWN_TO_SDK_VERSION; } UpdateAccessKeyRequest request = UpdateAccessKeyRequest.builder() .accessKeyId(accessId) .userName(username) .status(statusType) .build(); iam.updateAccessKey(request); System.out.printf( "Successfully updated the status of access key %s to" + "status %s for user %s", accessId, status, username); } catch (IamException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
请参阅上的完整示例
删除访问密钥
要永久删除访问密钥,请调用IamClient’s
deleteKey
方法,为其提供DeleteAccessKeyRequest
注意
密钥在删除后无法再检索或使用。要暂时停用密钥以便稍后可以再次激活,请改用updateAccessKey方法。
导入
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.iam.IamClient; import software.amazon.awssdk.services.iam.model.DeleteAccessKeyRequest; import software.amazon.awssdk.services.iam.model.IamException;
代码
public static void deleteKey(IamClient iam ,String username, String accessKey ) { try { DeleteAccessKeyRequest request = DeleteAccessKeyRequest.builder() .accessKeyId(accessKey) .userName(username) .build(); iam.deleteAccessKey(request); System.out.println("Successfully deleted access key " + accessKey + " from user " + username); } catch (IamException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
请参阅上的完整示例
更多信息
-
CreateAccessKey在 IAM API 参考中
-
ListAccessKeys在 IAM API 参考中
-
GetAccessKeyLastUsed在 IAM API 参考中
-
UpdateAccessKey在 IAM API 参考中
-
DeleteAccessKey在 IAM API 参考中