DisableKey搭配使用 Amazon SDK或 CLI - Amazon Key Management Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

DisableKey搭配使用 Amazon SDK或 CLI

以下代码示例演示如何使用 DisableKey

操作示例是大型程序的代码摘录,必须在上下文中运行。您可以在以下代码示例中查看此操作的上下文:

.NET
Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在中查找完整的示例,学习如何设置和运行 Amazon 代码示例存储库

using System; using System.Threading.Tasks; using Amazon.KeyManagementService; using Amazon.KeyManagementService.Model; /// <summary> /// Disable an AWS Key Management Service (AWS KMS) key and then retrieve /// the key's status to show that it has been disabled. /// </summary> public class DisableKey { public static async Task Main() { var client = new AmazonKeyManagementServiceClient(); // The identifier of the AWS KMS key to disable. You can use the // key Id or the Amazon Resource Name (ARN) of the AWS KMS key. var keyId = "1234abcd-12ab-34cd-56ef-1234567890ab"; var request = new DisableKeyRequest { KeyId = keyId, }; var response = await client.DisableKeyAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { // Retrieve information about the key to show that it has now // been disabled. var describeResponse = await client.DescribeKeyAsync(new DescribeKeyRequest { KeyId = keyId, }); Console.WriteLine($"{describeResponse.KeyMetadata.KeyId} - state: {describeResponse.KeyMetadata.KeyState}"); } } }
  • 有关API详细信息,请参阅DisableKey中的 Amazon SDK for .NET API参考

CLI
Amazon CLI

暂时禁用KMS密钥

以下示例使用disable-key命令禁用客户托管KMS密钥。要重新启用KMS密钥,请使用enable-key命令。

aws kms disable-key \ --key-id 1234abcd-12ab-34cd-56ef-1234567890ab

此命令不生成任何输出。

有关更多信息,请参阅中的启用和禁用密钥 Amazon 密钥管理服务开发人员指南

  • 有关API详细信息,请参阅DisableKey中的 Amazon CLI 命令参考

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。在中查找完整的示例,学习如何设置和运行 Amazon 代码示例存储库

/** * Asynchronously disables the specified AWS Key Management Service (KMS) key. * * @param keyId the ID or Amazon Resource Name (ARN) of the KMS key to be disabled * @return a CompletableFuture that, when completed, indicates that the key has been disabled successfully */ public CompletableFuture<Void> disableKeyAsync(String keyId) { DisableKeyRequest keyRequest = DisableKeyRequest.builder() .keyId(keyId) .build(); return getAsyncClient().disableKey(keyRequest) .thenRun(() -> { logger.info("Key {} has been disabled successfully",keyId); }) .exceptionally(throwable -> { throw new RuntimeException("Failed to disable key: " + keyId, throwable); }); }
  • 有关API详细信息,请参阅DisableKey中的 Amazon SDK for Java 2.x API参考

Kotlin
SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。在中查找完整的示例,学习如何设置和运行 Amazon 代码示例存储库

suspend fun disableKey(keyIdVal: String?) { val request = DisableKeyRequest { keyId = keyIdVal } KmsClient { region = "us-west-2" }.use { kmsClient -> kmsClient.disableKey(request) println("$keyIdVal was successfully disabled") } }
  • 有关API详细信息,请参阅DisableKey中的 Amazon SDK以供API参考 Kotlin。

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。在中查找完整的示例,学习如何设置和运行 Amazon 代码示例存储库

class KeyManager: def __init__(self, kms_client): self.kms_client = kms_client self.created_keys = [] def enable_disable_key(self, key_id): """ Disables and then enables a key. Gets the key state after each state change. """ answer = input("Do you want to disable and then enable that key (y/n)? ") if answer.lower() == "y": try: self.kms_client.disable_key(KeyId=key_id) key = self.kms_client.describe_key(KeyId=key_id)["KeyMetadata"] except ClientError as err: logging.error( "Couldn't disable key '%s'. Here's why: %s", key_id, err.response["Error"]["Message"], ) else: print(f"AWS KMS says your key state is: {key['KeyState']}.") try: self.kms_client.enable_key(KeyId=key_id) key = self.kms_client.describe_key(KeyId=key_id)["KeyMetadata"] except ClientError as err: logging.error( "Couldn't enable key '%s'. Here's why: %s", key_id, err.response["Error"]["Message"], ) else: print(f"AWS KMS says your key state is: {key['KeyState']}.")
  • 有关API详细信息,请参阅DisableKey中的 Amazon SDK供参考 Python (Boto3) API。

有关完整列表 Amazon SDK开发者指南和代码示例,请参阅使用 Amazon KMS 用一个 Amazon SDK。本主题还包括有关入门的信息以及有关先前SDK版本的详细信息。