There are more AWS SDK examples available in the AWS Doc SDK Examples
Use CreateVault
with an AWS SDK or CLI
The following code examples show how to use CreateVault
.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:
- .NET
-
- AWS SDK for .NET
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /// <summary> /// Create an Amazon S3 Glacier vault. /// </summary> /// <param name="vaultName">The name of the vault to create.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> CreateVaultAsync(string vaultName) { var request = new CreateVaultRequest { // Setting the AccountId to "-" means that // the account associated with the current // account will be used. AccountId = "-", VaultName = vaultName, }; var response = await _glacierService.CreateVaultAsync(request); Console.WriteLine($"Created {vaultName} at: {response.Location}"); return response.HttpStatusCode == HttpStatusCode.Created; }
-
For API details, see CreateVault in AWS SDK for .NET API Reference.
-
- CLI
-
- AWS CLI
-
The following command creates a new vault named
my-vault
:aws glacier create-vault --vault-name
my-vault
-
-account-id -Amazon Glacier requires an account ID argument when performing operations, but you can use a hyphen to specify the in-use account.
-
For API details, see CreateVault
in AWS CLI Command Reference.
-
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.glacier.GlacierClient; import software.amazon.awssdk.services.glacier.model.CreateVaultRequest; import software.amazon.awssdk.services.glacier.model.CreateVaultResponse; import software.amazon.awssdk.services.glacier.model.GlacierException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateVault { public static void main(String[] args) { final String usage = """ Usage: <vaultName> Where: vaultName - The name of the vault to create. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String vaultName = args[0]; GlacierClient glacier = GlacierClient.builder() .region(Region.US_EAST_1) .build(); createGlacierVault(glacier, vaultName); glacier.close(); } public static void createGlacierVault(GlacierClient glacier, String vaultName) { try { CreateVaultRequest vaultRequest = CreateVaultRequest.builder() .vaultName(vaultName) .build(); CreateVaultResponse createVaultResult = glacier.createVault(vaultRequest); System.out.println("The URI of the new vault is " + createVaultResult.location()); } catch (GlacierException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
-
For API details, see CreateVault in AWS SDK for Java 2.x API Reference.
-
- JavaScript
-
- SDK for JavaScript (v3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Create the client.
const { GlacierClient } = require("@aws-sdk/client-glacier"); // Set the AWS Region. const REGION = "REGION"; //Set the Redshift Service Object const glacierClient = new GlacierClient({ region: REGION }); export { glacierClient };
Create the vault.
// Load the SDK for JavaScript import { CreateVaultCommand } from "@aws-sdk/client-glacier"; import { glacierClient } from "./libs/glacierClient.js"; // Set the parameters const vaultname = "VAULT_NAME"; // VAULT_NAME const params = { vaultName: vaultname }; const run = async () => { try { const data = await glacierClient.send(new CreateVaultCommand(params)); console.log("Success, vault created!"); return data; // For unit tests. } catch (err) { console.log("Error"); } }; run();
-
For more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see CreateVault in AWS SDK for JavaScript API Reference.
-
- SDK for JavaScript (v2)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. // Load the SDK for JavaScript var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create a new service object var glacier = new AWS.Glacier({ apiVersion: "2012-06-01" }); // Call Glacier to create the vault glacier.createVault({ vaultName: "YOUR_VAULT_NAME" }, function (err) { if (!err) { console.log("Created vault!"); } });
-
For more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see CreateVault in AWS SDK for JavaScript API Reference.
-
- PowerShell
-
- Tools for PowerShell
-
Example 1: Creates a new vault for the user's account. As no value was supplied to the -AccountId parameter the cmdlets uses a default of "-" indicating the current account.
New-GLCVault -VaultName myvault
Output:
/01234567812/vaults/myvault
-
For API details, see CreateVault in AWS Tools for PowerShell Cmdlet Reference.
-
- Python
-
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. class GlacierWrapper: """Encapsulates Amazon S3 Glacier API operations.""" def __init__(self, glacier_resource): """ :param glacier_resource: A Boto3 Amazon S3 Glacier resource. """ self.glacier_resource = glacier_resource def create_vault(self, vault_name): """ Creates a vault. :param vault_name: The name to give the vault. :return: The newly created vault. """ try: vault = self.glacier_resource.create_vault(vaultName=vault_name) logger.info("Created vault %s.", vault_name) except ClientError: logger.exception("Couldn't create vault %s.", vault_name) raise else: return vault
-
For API details, see CreateVault in AWS SDK for Python (Boto3) API Reference.
-