Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .
As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.
Use ListUsageTotals com um AWS SDK
O código de exemplo a seguir mostra como usar ListUsageTotals.
Exemplos de ações são trechos de código de programas maiores e devem ser executados em contexto. É possível ver essa ação em contexto no seguinte exemplo de código:
- Java
-
- SDK para Java 2.x
-
/**
* Asynchronously lists Inspector2 usage totals using a paginator.
*
* @param accountIds optional list of account IDs
* @param maxResults maximum results per page
* @return CompletableFuture completed with formatted summary text
*/
public CompletableFuture<String> listUsageTotalsAsync(
List<String> accountIds,
int maxResults) {
logger.info("Starting usage totals paginator…");
ListUsageTotalsRequest.Builder builder = ListUsageTotalsRequest.builder()
.maxResults(maxResults);
if (accountIds != null && !accountIds.isEmpty()) {
builder.accountIds(accountIds);
}
ListUsageTotalsRequest request = builder.build();
ListUsageTotalsPublisher paginator = getAsyncClient().listUsageTotalsPaginator(request);
StringBuilder summaryBuilder = new StringBuilder();
return paginator.subscribe(response -> {
if (response.totals() != null && !response.totals().isEmpty()) {
response.totals().forEach(total -> {
if (total.usage() != null) {
total.usage().forEach(usage -> {
logger.info("Usage: {} = {}", usage.typeAsString(), usage.total());
summaryBuilder.append(usage.typeAsString())
.append(": ")
.append(usage.total())
.append("\n");
});
}
});
} else {
logger.info("Page contained no usage totals.");
}
}).thenRun(() -> logger.info("Successfully listed usage totals."))
.thenApply(v -> {
String summary = summaryBuilder.toString();
return summary.isEmpty() ? "No usage totals found." : summary;
}).exceptionally(ex -> {
Throwable cause = ex.getCause() != null ? ex.getCause() : ex;
if (cause instanceof ValidationException ve) {
throw new CompletionException(
"Validation error listing usage totals: %s".formatted(ve.getMessage()),
ve
);
}
throw new CompletionException("Failed to list usage totals", cause);
});
}