Sono disponibili altri esempi per SDK AWS nel repository GitHub della documentazione degli esempi per SDK AWS
Utilizzare EnableKey con un SDK AWS o una CLI
Gli esempi di codice seguenti mostrano come utilizzare EnableKey.
Gli esempi di operazioni sono estratti di codice da programmi più grandi e devono essere eseguiti nel contesto. Puoi vedere questa azione nel contesto nel seguente esempio di codice:
- .NET
-
- SDK per .NET
-
Nota
Ulteriori informazioni 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 informazioni dettagliate sull’API, consulta EnableKey nella documentazione di riferimento dell’API AWS SDK per .NET.
-
- CLI
-
- AWS CLI
-
Come abilitare una chiave KMS
L’esempio
enable-keyseguente abilita una chiave gestita dal cliente. Puoi utilizzare un comando come il seguente per abilitare una chiave KMS temporaneamente disabilitata utilizzando il comandodisable-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-id1234abcd-12ab-34cd-56ef-1234567890abQuesto comando non produce alcun output. Per verificare se la chiave KMS è abilitata, utilizza il comando
describe-key. Verifica i valori dei campiKeyStateeEnablede nell’outputdescribe-key.Per ulteriori informazioni, consulta Abilitazione e disabilitazione delle chiavi nella Guida per gli sviluppatori del Servizio AWS di gestione delle chiavi.
-
Per informazioni dettagliate sull’API, consulta EnableKey
in AWS CLI Command Reference.
-
- Java
-
- SDK per Java 2.x
-
Nota
Ulteriori informazioni 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 informazioni dettagliate sull’API, consulta EnableKey nella documentazione di riferimento dell’API AWS SDK for Java 2.x.
-
- Kotlin
-
- SDK per Kotlin
-
Nota
Ulteriori informazioni 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 informazioni dettagliate sull’API, consulta EnableKey
nella documentazione di riferimento dell’API AWS SDK per Kotlin.
-
- PHP
-
- SDK per PHP
-
Nota
Ulteriori informazioni 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 informazioni dettagliate sull’API, consulta EnableKey nella documentazione di riferimento dell’API AWS SDK per PHP.
-
- Python
-
- SDK per Python (Boto3)
-
Nota
Ulteriori informazioni 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 informazioni dettagliate sull’API, consulta EnableKey nella documentazione di riferimento dell’API AWS SDK per Python (Boto3).
-