ListVaults.java - AWS Code Sample

ListVaults.java

ListVaults.java demonstrates how to list all the Amazon Glacier vaults.

/* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.example.glacier; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.glacier.model.ListVaultsRequest; import software.amazon.awssdk.services.glacier.model.ListVaultsResponse; import software.amazon.awssdk.services.glacier.GlacierClient; import software.amazon.awssdk.services.glacier.model.DescribeVaultOutput; import software.amazon.awssdk.services.glacier.model.GlacierException; import java.util.List; /** * Lists your Amazon Glacier vaults * * This code expects that you have AWS credentials set up per: * http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html */ public class ListVaults { public static void main(String[] args) { // Create a GlacierClient object GlacierClient glacier = GlacierClient.builder() .region(Region.US_EAST_1) .build(); listAllVault(glacier); } public static void listAllVault(GlacierClient glacier) { boolean listComplete = false; String newMarker = null; int totalVaults = 0; System.out.println("Your Amazon Glacier vaults:"); try { while (!listComplete) { ListVaultsResponse response = null; if (newMarker != null) { ListVaultsRequest request = ListVaultsRequest.builder() .marker(newMarker) .build(); response = glacier.listVaults(request); } else { ListVaultsRequest request = ListVaultsRequest.builder() .build(); response = glacier.listVaults(request); } List<DescribeVaultOutput> vaultList = response.vaultList(); for (DescribeVaultOutput v: vaultList) { totalVaults += 1; System.out.println("* " + v.vaultName()); } // Check for further results newMarker = response.marker(); if (newMarker == null) { listComplete = true; } } if (totalVaults == 0) { System.out.println("No vaults found."); } } catch(GlacierException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

Sample Details

Service: glacier

Last tested: 4/17/2020

Author: scmacdon-aws

Type: full-example