Sono disponibili altri esempi AWS SDK nel repository AWS Doc SDK Examples. GitHub
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à.
Utilizzare DeleteKey con un SDK AWS
Gli esempi di codice seguenti mostrano come utilizzare DeleteKey.
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:
- Java
-
- SDK per Java 2.x
-
/**
* Deletes the specified key from the key-value store.
*
* @param keyName the name of the key to be deleted
* @return a {@link CompletableFuture} that completes when the key has been deleted
* @throws CompletionException if the key was not found or if an error occurred during the deletion process
*/
public CompletableFuture<Void> deleteKey(String keyName) {
DeleteKeyRequest keyRequest = DeleteKeyRequest.builder()
.keyName(keyName)
.build();
return getClient().deleteKey(keyRequest)
.whenComplete((response, exception) -> {
if (exception != null) {
Throwable cause = exception.getCause();
if (cause instanceof ResourceNotFoundException) {
throw new CompletionException("The key was not found.", cause);
}
throw new CompletionException("Failed to delete key: " + exception.getMessage(), exception);
}
logger.info("The key {} was deleted.", keyName);
})
.thenApply(response -> null);
}
- Kotlin
-
- SDK per Kotlin
-
/**
* Deletes the specified key from the key-value store.
*
* @param keyName the name of the key to be deleted
*/
suspend fun deleteKey(keyName: String) {
val keyRequest = DeleteKeyRequest {
this.keyName = keyName
}
LocationClient.fromEnvironment { region = "us-east-1" }.use { client ->
client.deleteKey(keyRequest)
println("The key $keyName was deleted.")
}
}