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 DeleteTracker con un SDK AWS
Gli esempi di codice seguenti mostrano come utilizzare DeleteTracker.
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 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 per 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 per 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.")
}
}