Contoh S3 Glacier menggunakan AWS SDK for .NET - AWS Contoh Kode SDK

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Contoh S3 Glacier menggunakan AWS SDK for .NET

Contoh kode berikut menunjukkan cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan gletser AWS SDK for .NET with S3.

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Meskipun tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks pada skenario terkait dan contoh lintas layanan.

Skenario adalah contoh kode yang menunjukkan cara menyelesaikan tugas tertentu dengan memanggil beberapa fungsi dalam layanan yang sama.

Setiap contoh menyertakan tautan ke GitHub, di mana Anda dapat menemukan petunjuk tentang cara mengatur dan menjalankan kode dalam konteks.

Memulai

Contoh kode berikut menunjukkan cara memulai menggunakan Amazon S3 Glacier.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

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}"); } } }
  • Untuk detail API, lihat ListVaultsdi Referensi AWS SDK for .NET API.

Tindakan

Contoh kode berikut menunjukkan cara menggunakanAddTagsToVault.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/// <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; }
  • Untuk detail API, lihat AddTagsToVaultdi Referensi AWS SDK for .NET API.

Contoh kode berikut menunjukkan cara menggunakanCreateVault.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/// <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; }
  • Untuk detail API, lihat CreateVaultdi Referensi AWS SDK for .NET API.

Contoh kode berikut menunjukkan cara menggunakanDescribeVault.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/// <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; }
  • Untuk detail API, lihat DescribeVaultdi Referensi AWS SDK for .NET API.

Contoh kode berikut menunjukkan cara menggunakanInitiateJob.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

Ambil arsip dari lemari besi. Contoh ini menggunakan ArchiveTransferManager kelas. Untuk detail API, lihat 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}%"); } }
  • Untuk detail API, lihat InitiateJobdi Referensi AWS SDK for .NET API.

Contoh kode berikut menunjukkan cara menggunakanListJobs.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/// <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; }
  • Untuk detail API, lihat ListJobsdi Referensi AWS SDK for .NET API.

Contoh kode berikut menunjukkan cara menggunakanListTagsForVault.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

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

Contoh kode berikut menunjukkan cara menggunakanListVaults.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/// <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; }
  • Untuk detail API, lihat ListVaultsdi Referensi AWS SDK for .NET API.

Contoh kode berikut menunjukkan cara menggunakanUploadArchive.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/// <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; } }
  • Untuk detail API, lihat UploadArchivedi Referensi AWS SDK for .NET API.