本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
ScheduleKeyDeletion
与 AWS SDK 或 CLI 配合使用
以下代码示例演示如何使用 ScheduleKeyDeletion
。
操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:
- CLI
-
- AWS CLI
-
计划删除客户托管的 KMS 密钥。
以下
schedule-key-deletion
示例计划在 15 天后删除指定的客户托管 KMS 密钥。--key-id
参数识别 KMS 密钥。此示例使用密钥 ARN 值,但您可以使用 KMS 密钥的密钥 ID 或 ARN。--pending-window-in-days
参数指定 7-30 天的等待期限。默认的等待期限为 30 天。此示例指定的值为 15,表示在命令完成 15 天后永久删除 KMS 密钥。 AWSaws kms schedule-key-deletion \ --key-id arn:aws:kms:us-west-2:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab \ --pending-window-in-days 15
响应包括密钥 ARN、密钥状态、等待期(
PendingWindowInDays
)和删除日期(以 Unix 时间表示)。要以当地时间查看删除日期,请使用 AWS KMS 控制台。无法在加密操作中使用密钥状态为PendingDeletion
的 KMS 密钥。{ "KeyId": "arn:aws:kms:us-west-2:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab", "DeletionDate": "2022-06-18T23:43:51.272000+00:00", "KeyState": "PendingDeletion", "PendingWindowInDays": 15 }
有关更多信息,请参阅《AWS 密钥管理服务开发人员指南》中的删除密钥。
-
有关 API 的详细信息,请参阅AWS CLI 命令参考ScheduleKeyDeletion
中的。
-
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Deletes a KMS key asynchronously. * * <p><strong>Warning:</strong> Deleting a KMS key is a destructive and potentially dangerous operation. * When a KMS key is deleted, all data that was encrypted under the KMS key becomes unrecoverable. * This means that any files, databases, or other data that were encrypted using the deleted KMS key * will become permanently inaccessible. Exercise extreme caution when deleting KMS keys.</p> * * @param keyId the ID of the KMS key to delete * @return a {@link CompletableFuture} that completes when the key deletion is scheduled */ public CompletableFuture<Void> deleteKeyAsync(String keyId) { ScheduleKeyDeletionRequest deletionRequest = ScheduleKeyDeletionRequest.builder() .keyId(keyId) .pendingWindowInDays(7) .build(); return getAsyncClient().scheduleKeyDeletion(deletionRequest) .thenRun(() -> { logger.info("Key {} will be deleted in 7 days", keyId); }) .exceptionally(throwable -> { throw new RuntimeException("Failed to schedule key deletion for key ID: " + keyId, throwable); }); }
-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考ScheduleKeyDeletion中的。
-
- PHP
-
- 适用于 PHP 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /*** * @param string $keyId * @param int $pendingWindowInDays * @return void */ public function scheduleKeyDeletion(string $keyId, int $pendingWindowInDays = 7) { try { $this->client->scheduleKeyDeletion([ 'KeyId' => $keyId, 'PendingWindowInDays' => $pendingWindowInDays, ]); }catch(KmsException $caught){ echo "There was a problem scheduling the key deletion: {$caught->getAwsErrorMessage()}\n"; throw $caught; } }
-
有关 API 的详细信息,请参阅 AWS SDK for PHP API 参考ScheduleKeyDeletion中的。
-
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class KeyManager: def __init__(self, kms_client): self.kms_client = kms_client self.created_keys = [] @classmethod def from_client(cls) -> "KeyManager": """ Creates a KeyManager instance with a default KMS client. :return: An instance of KeyManager initialized with the default KMS client. """ kms_client = boto3.client("kms") return cls(kms_client) def delete_key(self, key_id: str, window: int) -> None: """ Deletes a list of keys. Warning: Deleting a KMS key is a destructive and potentially dangerous operation. When a KMS key is deleted, all data that was encrypted under the KMS key is unrecoverable. :param key_id: The ARN or ID of the key to delete. :param window: The waiting period, in days, before the KMS key is deleted. """ try: self.kms_client.schedule_key_deletion( KeyId=key_id, PendingWindowInDays=window ) except ClientError as err: logging.error( "Couldn't delete key %s. Here's why: %s", key_id, err.response["Error"]["Message"], ) raise
-
有关 API 的详细信息,请参阅适用ScheduleKeyDeletion于 Python 的AWS SDK (Boto3) API 参考。
-
有关 S AWS DK 开发者指南和代码示例的完整列表,请参阅将此服务与 AWS SDK。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。