D'autres exemples de AWS SDK sont disponibles dans le référentiel AWS Doc SDK Examples GitHub .
Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
Utilisation DeleteKey avec un AWS SDK
Les exemples de code suivants illustrent comment utiliser DeleteKey.
Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action en contexte dans l’exemple de code suivant :
- Java
-
- SDK pour 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 pour 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.")
}
}