Creating a Vault in Amazon S3 Glacier Using the Amazon SDK for Java - Amazon S3 Glacier
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Creating a Vault in Amazon S3 Glacier Using the Amazon SDK for Java

The low-level API provides methods for all the vault operations, including creating and deleting vaults, getting a vault description, and getting a list of vaults created in a specific Amazon Web Services Region. The following are the steps to create a vault using the Amazon SDK for Java.

  1. Create an instance of the AmazonGlacierClient class (the client).

    You need to specify an Amazon Web Services Region in which you want to create a vault. All operations you perform using this client apply to that Amazon Web Services Region.

  2. Provide request information by creating an instance of the CreateVaultRequest class.

    Amazon S3 Glacier (S3 Glacier) requires you to provide a vault name and your account ID. If you don't provide an account ID, then the account ID associated with the credentials you provide to sign the request is used. For more information, see Using the Amazon SDK for Java with Amazon S3 Glacier.

  3. Run the createVault method by providing the request object as a parameter.

    The response S3 Glacier returns is available in the CreateVaultResult object.

The following Java code snippet illustrates the preceding steps. The snippet creates a vault in the us-west-2 Region. The Location it prints is the relative URI of the vault that includes your account ID, the Amazon Web Services Region, and the vault name.

AmazonGlacierClient client = new AmazonGlacierClient(credentials); client.setEndpoint(""); CreateVaultRequest request = new CreateVaultRequest() .withVaultName("*** provide vault name ***"); CreateVaultResult result = client.createVault(request); System.out.println("Created vault successfully: " + result.getLocation());

For information about the underlying REST API, see Create Vault (PUT vault).

Example: Creating a Vault Using the Amazon SDK for Java

The following Java code example creates a vault in the us-west-2 Region (for more information on Amazon Web Services Regions, see Accessing Amazon S3 Glacier). In addition, the code example retrieves the vault information, lists all vaults in the same Amazon Web Services Region, and then deletes the vault created.

For step-by-step instructions on how to run the following example, see Running Java Examples for Amazon S3 Glacier Using Eclipse.

import; import java.util.List; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import; import; import; import; import; import; import; import; import; public class AmazonGlacierVaultOperations { public static AmazonGlacierClient client; public static void main(String[] args) throws IOException { ProfileCredentialsProvider credentials = new ProfileCredentialsProvider(); client = new AmazonGlacierClient(credentials); client.setEndpoint(""); String vaultName = "examplevaultfordelete"; try { createVault(client, vaultName); describeVault(client, vaultName); listVaults(client); deleteVault(client, vaultName); } catch (Exception e) { System.err.println("Vault operation failed." + e.getMessage()); } } private static void createVault(AmazonGlacierClient client, String vaultName) { CreateVaultRequest createVaultRequest = new CreateVaultRequest() .withVaultName(vaultName); CreateVaultResult createVaultResult = client.createVault(createVaultRequest); System.out.println("Created vault successfully: " + createVaultResult.getLocation()); } private static void describeVault(AmazonGlacierClient client, String vaultName) { DescribeVaultRequest describeVaultRequest = new DescribeVaultRequest() .withVaultName(vaultName); DescribeVaultResult describeVaultResult = client.describeVault(describeVaultRequest); System.out.println("Describing the vault: " + vaultName); System.out.print( "CreationDate: " + describeVaultResult.getCreationDate() + "\nLastInventoryDate: " + describeVaultResult.getLastInventoryDate() + "\nNumberOfArchives: " + describeVaultResult.getNumberOfArchives() + "\nSizeInBytes: " + describeVaultResult.getSizeInBytes() + "\nVaultARN: " + describeVaultResult.getVaultARN() + "\nVaultName: " + describeVaultResult.getVaultName()); } private static void listVaults(AmazonGlacierClient client) { ListVaultsRequest listVaultsRequest = new ListVaultsRequest(); ListVaultsResult listVaultsResult = client.listVaults(listVaultsRequest); List<DescribeVaultOutput> vaultList = listVaultsResult.getVaultList(); System.out.println("\nDescribing all vaults (vault list):"); for (DescribeVaultOutput vault : vaultList) { System.out.println( "\nCreationDate: " + vault.getCreationDate() + "\nLastInventoryDate: " + vault.getLastInventoryDate() + "\nNumberOfArchives: " + vault.getNumberOfArchives() + "\nSizeInBytes: " + vault.getSizeInBytes() + "\nVaultARN: " + vault.getVaultARN() + "\nVaultName: " + vault.getVaultName()); } } private static void deleteVault(AmazonGlacierClient client, String vaultName) { DeleteVaultRequest request = new DeleteVaultRequest() .withVaultName(vaultName); client.deleteVault(request); System.out.println("Deleted vault: " + vaultName); } }