本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
EnableKey
搭配 AWS SDK或 使用 CLI
下列程式碼範例示範如何使用 EnableKey
。
動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:
- .NET
-
- AWS SDK for .NET
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 using System; using System.Threading.Tasks; using Amazon.KeyManagementService; using Amazon.KeyManagementService.Model; /// <summary> /// Enable an AWS Key Management Service (AWS KMS) key. /// </summary> public class EnableKey { public static async Task Main() { var client = new AmazonKeyManagementServiceClient(); // The identifier of the AWS KMS key to enable. 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 EnableKeyRequest { KeyId = keyId, }; var response = await client.EnableKeyAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { // Retrieve information about the key to show that it has now // been enabled. var describeResponse = await client.DescribeKeyAsync(new DescribeKeyRequest { KeyId = keyId, }); Console.WriteLine($"{describeResponse.KeyMetadata.KeyId} - state: {describeResponse.KeyMetadata.KeyState}"); } } }
-
如需API詳細資訊,請參閱 參考 EnableKey中的 。 AWS SDK for .NET API
-
- CLI
-
- AWS CLI
-
若要啟用KMS金鑰
下列
enable-key
範例會啟用客戶受管金鑰。您可以使用像這樣的命令來啟用您暫時停用的KMS金鑰,方法是使用disable-key
命令。您也可以使用它來啟用停用的金鑰,因為該KMS金鑰已排定刪除,且刪除已取消。若要指定KMS金鑰,請使用
key-id
參數。此範例使用金鑰 ID 值,但您可以在此命令中使用金鑰 ID 或金鑰ARN值。在執行此命令之前,請將範例金鑰 ID 取代為有效的金鑰 ID。
aws kms enable-key \ --key-id
1234abcd-12ab-34cd-56ef-1234567890ab
此命令不會產生輸出。若要確認KMS金鑰已啟用,請使用
describe-key
命令。請參閱describe-key
輸出中KeyState
和Enabled
欄位的值。如需詳細資訊,請參閱 AWS Key Management Service 開發人員指南中的啟用和停用金鑰。
-
如需API詳細資訊,請參閱 命令參考 EnableKey
中的 。 AWS CLI
-
- Java
-
- SDK 適用於 Java 2.x
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /** * Asynchronously enables the specified key. * * @param keyId the ID of the key to enable * @return a {@link CompletableFuture} that completes when the key has been enabled */ public CompletableFuture<Void> enableKeyAsync(String keyId) { EnableKeyRequest enableKeyRequest = EnableKeyRequest.builder() .keyId(keyId) .build(); CompletableFuture<EnableKeyResponse> responseFuture = getAsyncClient().enableKey(enableKeyRequest); responseFuture.whenComplete((response, exception) -> { if (exception == null) { logger.info("Key with ID [{}] has been enabled.", keyId); } else { if (exception instanceof KmsException kmsEx) { throw new RuntimeException("KMS error occurred while enabling key: " + kmsEx.getMessage(), kmsEx); } else { throw new RuntimeException("An unexpected error occurred while enabling key: " + exception.getMessage(), exception); } } }); return responseFuture.thenApply(response -> null); }
-
如需API詳細資訊,請參閱 參考 EnableKey中的 。 AWS SDK for Java 2.x API
-
- Kotlin
-
- SDK 適用於 Kotlin
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 suspend fun enableKey(keyIdVal: String?) { val request = EnableKeyRequest { keyId = keyIdVal } KmsClient { region = "us-west-2" }.use { kmsClient -> kmsClient.enableKey(request) println("$keyIdVal was successfully enabled.") } }
-
如需API詳細資訊,請參閱EnableKey
中的 AWS SDK for Kotlin API參考。
-
- PHP
-
- 適用於 PHP 的 SDK
-
注意
還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /*** * @param string $keyId * @return void */ public function enableKey(string $keyId) { try { $this->client->enableKey([ 'KeyId' => $keyId, ]); }catch(KmsException $caught){ if($caught->getAwsErrorMessage() == "NotFoundException"){ echo "The request was rejected because the specified entity or resource could not be found.\n"; } throw $caught; } }
-
如需API詳細資訊,請參閱 參考 EnableKey中的 。 AWS SDK for PHP API
-
- Python
-
- SDK for Python (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 enable_key(self, key_id: str) -> None: """ Enables a key. Gets the key state after each state change. :param key_id: The ARN or ID of the key to enable. """ try: self.kms_client.enable_key(KeyId=key_id) except ClientError as err: logging.error( "Couldn't enable key '%s'. Here's why: %s", key_id, err.response["Error"]["Message"], ) raise
-
如需API詳細資訊,請參閱 EnableKey 中的 AWS SDK for Python (Boto3) API參考 。
-
如需開發人員指南和程式碼範例的完整清單 AWS SDK,請參閱 將此服務與 搭配使用 AWS SDK。本主題也包含有關入門的資訊,以及先前SDK版本的詳細資訊。