使用 Amazon SDK for .NET 的 S3 Glacier 示例 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

使用 Amazon SDK for .NET 的 S3 Glacier 示例

以下代码示例演示如何将 Amazon SDK for .NET 与 S3 Glacier 结合使用来执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景是指显示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。

每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

开始使用

以下代码示例演示了如何开始使用 Amazon S3 Glacier。

Amazon SDK for .NET
注意

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

using Amazon.Glacier; using Amazon.Glacier.Model; namespace GlacierActions; public static class HelloGlacier { static async Task Main() { var glacierService = new AmazonGlacierClient(); Console.WriteLine("Hello Amazon Glacier!"); Console.WriteLine("Let's list your Glacier vaults:"); // You can use await and any of the async methods to get a response. // Let's get the vaults using a paginator. var glacierVaultPaginator = glacierService.Paginators.ListVaults( new ListVaultsRequest { AccountId = "-" }); await foreach (var vault in glacierVaultPaginator.VaultList) { Console.WriteLine($"{vault.CreationDate}:{vault.VaultName}, ARN:{vault.VaultARN}"); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ListVaults中的。

主题

操作

以下代码示例演示了如何添加标签到 Amazon S3 Glacier 保管库中。

Amazon SDK for .NET
注意

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

/// <summary> /// Add tags to the items in an Amazon S3 Glacier vault. /// </summary> /// <param name="vaultName">The name of the vault to add tags to.</param> /// <param name="key">The name of the object to tag.</param> /// <param name="value">The tag value to add.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> AddTagsToVaultAsync(string vaultName, string key, string value) { var request = new AddTagsToVaultRequest { Tags = new Dictionary<string, string> { { key, value }, }, AccountId = "-", VaultName = vaultName, }; var response = await _glacierService.AddTagsToVaultAsync(request); return response.HttpStatusCode == HttpStatusCode.NoContent; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考AddTagsToVault中的。

以下代码示例演示了如何创建 Amazon S3 Glacier 文件库。

Amazon SDK for .NET
注意

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

/// <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 的详细信息,请参阅 Amazon SDK for .NETAPI 参考CreateVault中的。

以下代码示例演示了如何描述 Amazon S3 Glacier 保管库。

Amazon SDK for .NET
注意

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

/// <summary> /// Describe an Amazon S3 Glacier vault. /// </summary> /// <param name="vaultName">The name of the vault to describe.</param> /// <returns>The Amazon Resource Name (ARN) of the vault.</returns> public async Task<string> DescribeVaultAsync(string vaultName) { var request = new DescribeVaultRequest { AccountId = "-", VaultName = vaultName, }; var response = await _glacierService.DescribeVaultAsync(request); // Display the information about the vault. Console.WriteLine($"{response.VaultName}\tARN: {response.VaultARN}"); Console.WriteLine($"Created on: {response.CreationDate}\tNumber of Archives: {response.NumberOfArchives}\tSize (in bytes): {response.SizeInBytes}"); if (response.LastInventoryDate != DateTime.MinValue) { Console.WriteLine($"Last inventory: {response.LastInventoryDate}"); } return response.VaultARN; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DescribeVault中的。

以下代码示例演示如何下载 Amazon S3 Glacier 存档。

Amazon SDK for .NET
注意

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

此示例使用 ArchiveTransferManager 类。有关 API 的详细信息,请参阅ArchiveTransferManager

/// <summary> /// Download an archive from an Amazon S3 Glacier vault using the Archive /// Transfer Manager. /// </summary> /// <param name="vaultName">The name of the vault containing the object.</param> /// <param name="archiveId">The Id of the archive to download.</param> /// <param name="localFilePath">The local directory where the file will /// be stored after download.</param> /// <returns>Async Task.</returns> public async Task<bool> DownloadArchiveWithArchiveManagerAsync(string vaultName, string archiveId, string localFilePath) { try { var manager = new ArchiveTransferManager(_glacierService); var options = new DownloadOptions { StreamTransferProgress = Progress!, }; // Download an archive. Console.WriteLine("Initiating the archive retrieval job and then polling SQS queue for the archive to be available."); Console.WriteLine("When the archive is available, downloading will begin."); await manager.DownloadAsync(vaultName, archiveId, localFilePath, options); return true; } catch (AmazonGlacierException ex) { Console.WriteLine(ex.Message); return false; } } /// <summary> /// Event handler to track the progress of the Archive Transfer Manager. /// </summary> /// <param name="sender">The object that raised the event.</param> /// <param name="args">The argument values from the object that raised the /// event.</param> static void Progress(object sender, StreamTransferProgressArgs args) { if (args.PercentDone != _currentPercentage) { _currentPercentage = args.PercentDone; Console.WriteLine($"Downloaded {_currentPercentage}%"); } }

以下代码示例演示如何列出 Amazon S3 Glacier 任务。

Amazon SDK for .NET
注意

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

/// <summary> /// List Amazon S3 Glacier jobs. /// </summary> /// <param name="vaultName">The name of the vault to list jobs for.</param> /// <returns>A list of Amazon S3 Glacier jobs.</returns> public async Task<List<GlacierJobDescription>> ListJobsAsync(string vaultName) { var request = new ListJobsRequest { // Using a hyphen "-" for the Account Id will // cause the SDK to use the Account Id associated // with the current account. AccountId = "-", VaultName = vaultName, }; var response = await _glacierService.ListJobsAsync(request); return response.JobList; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ListJobs中的。

以下代码示例演示了如何列出 Amazon S3 Glacier 保管库的标签。

Amazon SDK for .NET
注意

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

/// <summary> /// List tags for an Amazon S3 Glacier vault. /// </summary> /// <param name="vaultName">The name of the vault to list tags for.</param> /// <returns>A dictionary listing the tags attached to each object in the /// vault and its tags.</returns> public async Task<Dictionary<string, string>> ListTagsForVaultAsync(string vaultName) { var request = new ListTagsForVaultRequest { // Using a hyphen "-" for the Account Id will // cause the SDK to use the Account Id associated // with the default user. AccountId = "-", VaultName = vaultName, }; var response = await _glacierService.ListTagsForVaultAsync(request); return response.Tags; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ListTagsForVault中的。

以下代码示例演示如何列出 Amazon S3 Glacier 文件库。

Amazon SDK for .NET
注意

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

/// <summary> /// List the Amazon S3 Glacier vaults associated with the current account. /// </summary> /// <returns>A list containing information about each vault.</returns> public async Task<List<DescribeVaultOutput>> ListVaultsAsync() { var glacierVaultPaginator = _glacierService.Paginators.ListVaults( new ListVaultsRequest { AccountId = "-" }); var vaultList = new List<DescribeVaultOutput>(); await foreach (var vault in glacierVaultPaginator.VaultList) { vaultList.Add(vault); } return vaultList; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ListVaults中的。

以下代码示例演示了如何将存档上传到 Amazon S3 Glacier 文件库。

Amazon SDK for .NET
注意

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

/// <summary> /// Upload an object to an Amazon S3 Glacier vault. /// </summary> /// <param name="vaultName">The name of the Amazon S3 Glacier vault to upload /// the archive to.</param> /// <param name="archiveFilePath">The file path of the archive to upload to the vault.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<string> UploadArchiveWithArchiveManager(string vaultName, string archiveFilePath) { try { var manager = new ArchiveTransferManager(_glacierService); // Upload an archive. var response = await manager.UploadAsync(vaultName, "upload archive test", archiveFilePath); return response.ArchiveId; } catch (AmazonGlacierException ex) { Console.WriteLine(ex.Message); return string.Empty; } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考UploadArchive中的。