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 DeleteTracker avec un AWS SDK
Les exemples de code suivants illustrent comment utiliser DeleteTracker.
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 a tracker with the specified name.
*
* @param trackerName the name of the tracker to be deleted
* @return a {@link CompletableFuture} that completes when the tracker has been deleted
* @throws CompletionException if an error occurs while deleting the tracker
* - if the tracker was not found, a {@link ResourceNotFoundException} is thrown wrapped in the CompletionException
* - if any other error occurs, a generic CompletionException is thrown with the error message
*/
public CompletableFuture<Void> deleteTracker(String trackerName) {
DeleteTrackerRequest trackerRequest = DeleteTrackerRequest.builder()
.trackerName(trackerName)
.build();
return getClient().deleteTracker(trackerRequest)
.whenComplete((response, exception) -> {
if (exception != null) {
Throwable cause = exception.getCause();
if (cause instanceof ResourceNotFoundException) {
throw new CompletionException("The tracker was not found.", cause);
}
throw new CompletionException("Failed to delete the tracker: " + exception.getMessage(), exception);
}
logger.info("The tracker {} was deleted.", trackerName);
})
.thenApply(response -> null); // Ensures CompletableFuture<Void>
}
- JavaScript
-
- SDK pour JavaScript (v3)
-
import { fileURLToPath } from "node:url";
import {
DeleteTrackerCommand,
LocationClient,
ResourceNotFoundException,
} from "@aws-sdk/client-location";
import data from "./inputs.json" with { type: "json" };
const region = "eu-west-1";
export const main = async () => {
const deleteTrackerParams = {
TrackerName: `${data.inputs.trackerName}`,
};
try {
const locationClient = new LocationClient({ region: region });
const command = new DeleteTrackerCommand(deleteTrackerParams);
const response = await locationClient.send(command);
console.log("Tracker deleted.");
} catch (caught) {
if (caught instanceof ResourceNotFoundException) {
console.error(`${data.inputs.trackerName} tracker not found.`);
return;
}
}
};
- Kotlin
-
- SDK pour Kotlin
-
/**
* Deletes a tracker with the specified name.
* @param trackerName the name of the tracker to be deleted
*/
suspend fun deleteTracker(trackerName: String) {
val trackerRequest = DeleteTrackerRequest {
this.trackerName = trackerName
}
LocationClient { region = "us-east-1" }.use { client ->
client.deleteTracker(trackerRequest)
println("The tracker $trackerName was deleted.")
}
}