与 AWS SDK或CreateVault一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或CreateVault一起使用 CLI

以下代码示例演示如何使用 CreateVault

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
AWS SDK for .NET
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

/// <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; }
  • 有关API详细信息,请参阅 “AWS SDK for .NET API参考 CreateVault” 中的。

CLI
AWS CLI

以下命令创建名为 my-vault 的新文件库:

aws glacier create-vault --vault-name my-vault --account-id -

Amazon Glacier 在执行操作时需要一个账户 ID 参数,但您可以使用连字符来指定正在使用的账户。

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

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); } } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 CreateVault” 中的。

JavaScript
SDK对于 JavaScript (v3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

创建客户端。

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 };

创建文件库。

// 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();
SDK对于 JavaScript (v2)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

// 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!"); } });
PowerShell
用于 PowerShell

示例 1:为用户的账户创建新的保管库。由于未向-AccountId 参数提供任何值,因此 cmdlet 使用默认值 “-” 来表示当前账户。

New-GLCVault -VaultName myvault

输出:

/01234567812/vaults/myvault
  • 有关API详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考CreateVault中的。

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

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
  • 有关API详细信息,请参阅CreateVault中的 AWS SDKPython (Boto3) API 参考。