View a markdown version of this page

Uso EnableKey con un AWS SDK o CLI - AWS Key Management Service

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Uso EnableKey con un AWS SDK o CLI

I seguenti esempi di codice mostrano come usare EnableKey.

Gli esempi di operazioni sono estratti di codice da programmi più grandi e devono essere eseguiti nel contesto. È possibile visualizzare questa operazione nel contesto nel seguente esempio di codice:

.NET
SDK per .NET
Nota

C'è di più su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel repository di esempi di codice 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}"); } } }
  • Per i dettagli sull'API, vedi EnableKey nel AWS SDK per .NET Riferimento API.

CLI
AWS CLI

Come abilitare una chiave KMS

L’esempio enable-key seguente abilita una chiave gestita dal cliente. Puoi utilizzare un comando come il seguente per abilitare una chiave KMS temporaneamente disabilitata utilizzando il comando disable-key. Puoi anche usarlo per abilitare una chiave KMS disabilitata perché era stata pianificata per l’eliminazione e l’eliminazione è stata annullata.

Per specificare la chiave KMS, utilizza il parametro key-id. Questo esempio utilizza un valore per l’ID chiave, ma in questo comando puoi utilizzare un ID o un ARN di chiave.

Prima di eseguire questo comando, sostituisci l’ID della chiave di esempio con un ID valido.

aws kms enable-key \ --key-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111

Questo comando non produce output. Per verificare se la chiave KMS è abilitata, utilizza il comando describe-key. Verifica i valori dei campi KeyState e Enabled nell’output di describe-key.

Per ulteriori informazioni, consulta Abilitazione e disabilitazione delle chiavi nella Guida per gli sviluppatori del Servizio AWS di gestione delle chiavi.

  • Per i dettagli sull'API, vedi EnableKey nel AWS CLI Riferimento al comando.

Java
SDK per Java 2.x
Nota

C'è di più su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel repository di esempi di codice 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); }
  • Per i dettagli sull'API, vedi EnableKey nel AWS SDK for Java 2.x Riferimento API.

Kotlin
SDK per Kotlin
Nota

C'è di più su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel repository di esempi di codice AWS.

suspend fun enableKey(keyIdVal: String?) { val request = EnableKeyRequest { keyId = keyIdVal } KmsClient.fromEnvironment { region = "us-west-2" }.use { kmsClient -> kmsClient.enableKey(request) println("$keyIdVal was successfully enabled.") } }
  • Per i dettagli sull'API, vedi EnableKey nel AWS Riferimento all'API SDK per Kotlin.

PHP
SDK per PHP
Nota

C'è di più su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel repository di esempi di codice 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; } }
  • Per i dettagli sull'API, vedi EnableKey nel AWS SDK per PHP Riferimento API.

Python
SDK for Python (Boto3)
Nota

C'è di più su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel repository di esempi di codice 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
  • Per i dettagli sull'API, vedi EnableKey nel AWS Riferimento alle API SDK per Python (Boto3).

SAP ABAP
SDK per SAP ABAP
Nota

C'è altro su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel repository di esempi di codice AWS.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' lo_kms->enablekey( iv_keyid = iv_key_id ). MESSAGE 'KMS key enabled successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Key not found.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • Per i dettagli sull'API, vedi EnableKey nel AWS Riferimento all'API SDK per SAP ABAP.

Per un elenco completo di AWS Guide per sviluppatori SDK ed esempi di codice, consulta L'utilizzo di questo servizio con un AWS SDK. Questo argomento include anche informazioni su come iniziare e dettagli sulle versioni precedenti dell'SDK.