Mengunduh Arsip di Amazon S3 Glacier Menggunakan AWS SDK for .NET - Amazon S3 Glacier

Halaman ini hanya untuk pelanggan lama layanan S3 Glacier menggunakan Vaults dan REST API asli dari tahun 2012.

Jika Anda mencari solusi penyimpanan arsip, kami sarankan untuk menggunakan kelas penyimpanan S3 Glacier di Amazon S3, Pengambilan Instan Gletser S3, Pengambilan Fleksibel Gletser S3, dan S3 Glacier Deep Archive. Untuk mempelajari lebih lanjut tentang opsi penyimpanan ini, lihat Kelas penyimpanan S3 Glacier dan Penyimpanan data jangka panjang menggunakan kelas penyimpanan S3 Glacier di Panduan Pengguna Amazon S3. Kelas penyimpanan ini menggunakan Amazon S3 API, tersedia di semua wilayah, dan dapat dikelola dalam konsol Amazon S3. Mereka menawarkan fitur seperti Analisis Biaya Penyimpanan, Lensa Penyimpanan, fitur keamanan termasuk beberapa opsi enkripsi, dan banyak lagi.

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

Mengunduh Arsip di Amazon S3 Glacier Menggunakan AWS SDK for .NET

API tingkat tinggi dan tingkat rendah yang disediakan oleh Amazon SDK for .NET menyediakan metode untuk mengunduh arsip.

Mengunduh Arsip Menggunakan API Tingkat Tinggi dari AWS SDK for .NET

Kelas ArchiveTransferManager dari API tingkat tinggi menyediakan metode Download yang dapat Anda gunakan untuk mengunduh arsip.

penting

Kelas ArchiveTransferManager membuat topik Amazon Simple Notification Service (Amazon SNS) dan antrean Amazon Simple Queue Service (Amazon SQS) yang berlangganan topik tersebut. Kelas tersebut selanjutnya memulai tugas pengambilan arsip dan melakukan polling antrean untuk arsip yang akan tersedia. Ketika arsip tersedia, unduhan dimulai. Untuk informasi tentang waktu pengambilan, lihat Opsi Pengambilan Arsip

Contoh: Mengunduh Arsip Menggunakan API Tingkat Tinggi dari AWS SDK for .NET

Contoh kode C# berikut mengunduh arsip dari vault (examplevault) di Wilayah US West (Oregon).

Untuk petunjuk langkah demi langkah tentang cara menjalankan contoh ini, lihat Menjalankan Contoh Kode. Anda perlu memperbarui kode seperti yang ditunjukkan dengan ID arsip yang ada dan jalur file lokal tempat Anda ingin menyimpan arsip yang diunduh.

using System; using Amazon.Glacier; using Amazon.Glacier.Transfer; using Amazon.Runtime; namespace glacier.amazon.com.docsamples { class ArchiveDownloadHighLevel { static string vaultName = "examplevault"; static string archiveId = "*** Provide archive ID ***"; static string downloadFilePath = "*** Provide the file name and path to where to store the download ***"; public static void Main(string[] args) { try { var manager = new ArchiveTransferManager(Amazon.RegionEndpoint.USWest2); var options = new DownloadOptions(); options.StreamTransferProgress += ArchiveDownloadHighLevel.progress; // Download an archive. Console.WriteLine("Intiating the archive retrieval job and then polling SQS queue for the archive to be available."); Console.WriteLine("Once the archive is available, downloading will begin."); manager.Download(vaultName, archiveId, downloadFilePath, options); Console.WriteLine("To continue, press Enter"); Console.ReadKey(); } catch (AmazonGlacierException e) { Console.WriteLine(e.Message); } catch (AmazonServiceException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("To continue, press Enter"); Console.ReadKey(); } static int currentPercentage = -1; static void progress(object sender, StreamTransferProgressArgs args) { if (args.PercentDone != currentPercentage) { currentPercentage = args.PercentDone; Console.WriteLine("Downloaded {0}%", args.PercentDone); } } } }

Mengunduh Arsip Menggunakan API Tingkat Rendah dari AWS SDK for .NET

Berikut adalah langkah-langkah untuk mengunduh arsip Amazon S3 Glacier (S3 Glacier) menggunakan API tingkat rendah dari AWS SDK for .NET.

  1. Buat instans dari kelas AmazonGlacierClient (klien).

    Anda harus menentukan Wilayah AWS tempat Anda ingin mengunduh arsip. Semua operasi yang Anda lakukan menggunakan klien ini berlaku untuk Wilayah AWS tersebut.

  2. Mulai tugas archive-retrieval dengan menjalankan metode InitiateJob.

    Anda memberikan informasi tugas, seperti ID arsip dari arsip yang ingin Anda unduh dan topik Amazon SNS opsional tempat Anda ingin S3 Glacier mengirim pesan penyelesaian tugas, dengan membuat instans kelas InitiateJobRequest. S3 Glacier mengembalikan ID tugas dalam respons. Respons tersedia dalam instans dari kelas InitiateJobResponse.

    AmazonGlacierClient client; client = new AmazonGlacierClient(Amazon.RegionEndpoint.USWest2); InitiateJobRequest initJobRequest = new InitiateJobRequest() { VaultName = vaultName, JobParameters = new JobParameters() { Type = "archive-retrieval", ArchiveId = "*** Provide archive id ***", SNSTopic = "*** Provide Amazon SNS topic ARN ***", } }; InitiateJobResponse initJobResponse = client.InitiateJob(initJobRequest); string jobId = initJobResponse.JobId;

    Anda secara opsional dapat menentukan rentang byte untuk meminta S3 Glacier mempersiapkan hanya sebagian dari arsip seperti yang diperlihatkan dalam permintaan berikut. Permintaan menetapkan S3 Glacier untuk mempersiapkan hanya 1 MB hingga 2 MB bagian arsip.

    AmazonGlacierClient client; client = new AmazonGlacierClient(Amazon.RegionEndpoint.USWest2); InitiateJobRequest initJobRequest = new InitiateJobRequest() { VaultName = vaultName, JobParameters = new JobParameters() { Type = "archive-retrieval", ArchiveId = "*** Provide archive id ***", SNSTopic = "*** Provide Amazon SNS topic ARN ***", } }; // Specify byte range. int ONE_MEG = 1048576; initJobRequest.JobParameters.RetrievalByteRange = string.Format("{0}-{1}", ONE_MEG, 2 * ONE_MEG -1); InitiateJobResponse initJobResponse = client.InitiateJob(initJobRequest); string jobId = initJobResponse.JobId;
  3. Tunggu hingga tugas selesai.

    Anda harus menunggu hingga output tugas siap diunduh. Jika Anda menetapkan konfigurasi notifikasi di vault yang mengidentifikasi topik Amazon Simple Notification Service (Amazon SNS) atau menentukan topik Amazon SNS ketika Anda memulai tugas, S3 Glacier mengirimkan pesan ke topik tersebut setelah menyelesaikan tugas. Contoh kode yang diberikan dalam bagian berikut menggunakan Amazon SNS untuk S3 Glacier untuk menerbitkan pesan.

    Anda juga dapat melakukan polling pada S3 Glacier dengan memanggil metode DescribeJob untuk menentukan status penyelesaian tugas. Meskipun, menggunakan topik Amazon SNS untuk notifikasi adalah pendekatan yang disarankan.

  4. Unduh output tugas (data arsip) dengan menjalankan metode GetJobOutput.

    Anda memberikan informasi permintaan seperti ID tugas dan nama vault dengan membuat instans dari kelas GetJobOutputRequest. Output yang dikembalikan S3 Glacier tersedia di objek GetJobOutputResponse.

    GetJobOutputRequest getJobOutputRequest = new GetJobOutputRequest() { JobId = jobId, VaultName = vaultName }; GetJobOutputResponse getJobOutputResponse = client.GetJobOutput(getJobOutputRequest); using (Stream webStream = getJobOutputResponse.Body) { using (Stream fileToSave = File.OpenWrite(fileName)) { CopyStream(webStream, fileToSave); } }

    Potongan kode sebelumnya mengunduh seluruh output tugas. Anda secara opsional dapat mengambil hanya sebagian output, atau mengunduh seluruh output dalam potongan yang lebih kecil dengan menentukan rentang byte di GetJobOutputRequest Anda.

    GetJobOutputRequest getJobOutputRequest = new GetJobOutputRequest() { JobId = jobId, VaultName = vaultName }; getJobOutputRequest.SetRange(0, 1048575); // Download only the first 1 MB chunk of the output.

    Dalam merespons panggilan GetJobOutput, S3 Glacier mengembalikan checksum bagian data yang Anda unduh, jika kondisi tertentu terpenuhi. Untuk informasi selengkapnya, lihat Menerima Checksum Saat Mengunduh Data.

    Untuk memastikan tidak ada kesalahan dalam unduhan, Anda selanjutnya dapat menghitung checksum di sisi klien dan membandingkannya dengan checksum yang dikirim S3 Glacier dalam respons.

    Untuk tugas pengambilan arsip dengan rentang opsional yang ditentukan, ketika Anda mendapatkan deskripsi tugas, deskripsi tersebut termasuk checksum dari rentang yang Anda ambil (SHA256TreeHash). Anda dapat menggunakan nilai ini untuk memverifikasi lebih lanjut akurasi seluruh rentang byte yang Anda unduh selanjutnya. Misalnya, jika Anda memulai tugas untuk mengambil rentang arsip hash pohon selaras, lalu mengunduh output dalam potongan sehingga masing-masing permintaan GetJobOutput Anda mengembalikan checksum, Anda dapat menghitung checksum setiap bagian yang Anda unduh di sisi klien, lalu menghitung hash pohon. Anda dapat membandingkannya dengan pengembalian S3 Glacier checksum sebagai respons atas permintaan deskripsi tugas Anda untuk memverifikasi seluruh rentang byte yang sudah Anda unduh sama dengan rentang byte yang disimpan di S3 Glacier.

    Untuk contoh kerja, lihat Contoh 2: Mengambil Arsip Menggunakan API Tingkat Rendah dariAWS SDK for .NET—Unduh Output dalam Potongan.

Contoh 1: Mengambil Arsip Menggunakan API Tingkat Rendah dariAWS SDK for .NET

Contoh kode C# berikut mengunduh arsip dari vault yang ditentukan. Setelah tugas selesai, contoh mengunduh seluruh output dalam satu panggilan GetJobOutput. Untuk contoh mengunduh output dalam potongan, lihat Contoh 2: Mengambil Arsip Menggunakan API Tingkat Rendah dariAWS SDK for .NET—Unduh Output dalam Potongan.

Contoh tersebut melakukan tugas-tugas berikut:

  • Menyiapkan topik Amazon Simple Notification Service (Amazon SNS)

    S3 Glacier mengirimkan notifikasi ke topik ini setelah menyelesaikan tugas.

  • Menyiapkan antrean Amazon Simple Queue Service (Amazon SQS).

    Contoh melampirkan kebijakan ke antrean untuk mengizinkan topik Amazon SNS mengirim pesan.

  • Memulai tugas untuk mengunduh arsip yang ditentukan.

    Dalam permintaan tugas, contoh menentukan topik Amazon SNS sehingga S3 Glacier dapat mengirim pesan setelah menyelesaikan tugas.

  • Memeriksa antrean Amazon SQS secara berkala untuk pesan.

    Jika ada pesan, urai JSON dan periksa apakah tugas berhasil diselesaikan. Jika ya, unduh arsipnya. Contoh kode menggunakan pustaka JSON.NET (lihat JSON.NET) untuk mengurai JSON.

  • Membersihkan dengan menghapus topik Amazon SNS dan antrean Amazon SQS yang dibuat.

using System; using System.Collections.Generic; using System.IO; using System.Threading; using Amazon.Glacier; using Amazon.Glacier.Model; using Amazon.Runtime; using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; using Amazon.SQS; using Amazon.SQS.Model; using Newtonsoft.Json; namespace glacier.amazon.com.docsamples { class ArchiveDownloadLowLevelUsingSNSSQS { static string topicArn; static string queueUrl; static string queueArn; static string vaultName = "*** Provide vault name ***"; static string archiveID = "*** Provide archive ID ***"; static string fileName = "*** Provide the file name and path to where to store downloaded archive ***"; static AmazonSimpleNotificationServiceClient snsClient; static AmazonSQSClient sqsClient; const string SQS_POLICY = "{" + " \"Version\" : \"2012-10-17\"," + " \"Statement\" : [" + " {" + " \"Sid\" : \"sns-rule\"," + " \"Effect\" : \"Allow\"," + " \"Principal\" : {\"Service\" : \"sns.amazonaws.com\" }," + " \"Action\" : \"sqs:SendMessage\"," + " \"Resource\" : \"{QueueArn}\"," + " \"Condition\" : {" + " \"ArnLike\" : {" + " \"aws:SourceArn\" : \"{TopicArn}\"" + " }" + " }" + " }" + " ]" + "}"; public static void Main(string[] args) { AmazonGlacierClient client; try { using (client = new AmazonGlacierClient(Amazon.RegionEndpoint.USWest2)) { Console.WriteLine("Setup SNS topic and SQS queue."); SetupTopicAndQueue(); Console.WriteLine("To continue, press Enter"); Console.ReadKey(); Console.WriteLine("Retrieving..."); RetrieveArchive(client); } Console.WriteLine("Operations successful. To continue, press Enter"); Console.ReadKey(); } catch (AmazonGlacierException e) { Console.WriteLine(e.Message); } catch (AmazonServiceException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } finally { // Delete SNS topic and SQS queue. snsClient.DeleteTopic(new DeleteTopicRequest() { TopicArn = topicArn }); sqsClient.DeleteQueue(new DeleteQueueRequest() { QueueUrl = queueUrl }); } } static void SetupTopicAndQueue() { snsClient = new AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.USWest2); sqsClient = new AmazonSQSClient(Amazon.RegionEndpoint.USWest2); long ticks = DateTime.Now.Ticks; topicArn = snsClient.CreateTopic(new CreateTopicRequest { Name = "GlacierDownload-" + ticks }).TopicArn; Console.Write("topicArn: "); Console.WriteLine(topicArn); CreateQueueRequest createQueueRequest = new CreateQueueRequest(); createQueueRequest.QueueName = "GlacierDownload-" + ticks; CreateQueueResponse createQueueResponse = sqsClient.CreateQueue(createQueueRequest); queueUrl = createQueueResponse.QueueUrl; Console.Write("QueueURL: "); Console.WriteLine(queueUrl); GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(); getQueueAttributesRequest.AttributeNames = new List<string> { "QueueArn" }; getQueueAttributesRequest.QueueUrl = queueUrl; GetQueueAttributesResponse response = sqsClient.GetQueueAttributes(getQueueAttributesRequest); queueArn = response.QueueARN; Console.Write("QueueArn: "); Console.WriteLine(queueArn); // Setup the Amazon SNS topic to publish to the SQS queue. snsClient.Subscribe(new SubscribeRequest() { Protocol = "sqs", Endpoint = queueArn, TopicArn = topicArn }); // Add policy to the queue so SNS can send messages to the queue. var policy = SQS_POLICY.Replace("{TopicArn}", topicArn).Replace("{QueueArn}", queueArn); sqsClient.SetQueueAttributes(new SetQueueAttributesRequest() { QueueUrl = queueUrl, Attributes = new Dictionary<string, string> { { QueueAttributeName.Policy, policy } } }); } static void RetrieveArchive(AmazonGlacierClient client) { // Initiate job. InitiateJobRequest initJobRequest = new InitiateJobRequest() { VaultName = vaultName, JobParameters = new JobParameters() { Type = "archive-retrieval", ArchiveId = archiveID, Description = "This job is to download archive.", SNSTopic = topicArn, } }; InitiateJobResponse initJobResponse = client.InitiateJob(initJobRequest); string jobId = initJobResponse.JobId; // Check queue for a message and if job completed successfully, download archive. ProcessQueue(jobId, client); } private static void ProcessQueue(string jobId, AmazonGlacierClient client) { ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest() { QueueUrl = queueUrl, MaxNumberOfMessages = 1 }; bool jobDone = false; while (!jobDone) { Console.WriteLine("Poll SQS queue"); ReceiveMessageResponse receiveMessageResponse = sqsClient.ReceiveMessage(receiveMessageRequest); if (receiveMessageResponse.Messages.Count == 0) { Thread.Sleep(10000 * 60); continue; } Console.WriteLine("Got message"); Message message = receiveMessageResponse.Messages[0]; Dictionary<string, string> outerLayer = JsonConvert.DeserializeObject<Dictionary<string, string>>(message.Body); Dictionary<string, object> fields = JsonConvert.DeserializeObject<Dictionary<string, object>>(outerLayer["Message"]); string statusCode = fields["StatusCode"] as string; if (string.Equals(statusCode, GlacierUtils.JOB_STATUS_SUCCEEDED, StringComparison.InvariantCultureIgnoreCase)) { Console.WriteLine("Downloading job output"); DownloadOutput(jobId, client); // Save job output to the specified file location. } else if (string.Equals(statusCode, GlacierUtils.JOB_STATUS_FAILED, StringComparison.InvariantCultureIgnoreCase)) Console.WriteLine("Job failed... cannot download the archive."); jobDone = true; sqsClient.DeleteMessage(new DeleteMessageRequest() { QueueUrl = queueUrl, ReceiptHandle = message.ReceiptHandle }); } } private static void DownloadOutput(string jobId, AmazonGlacierClient client) { GetJobOutputRequest getJobOutputRequest = new GetJobOutputRequest() { JobId = jobId, VaultName = vaultName }; GetJobOutputResponse getJobOutputResponse = client.GetJobOutput(getJobOutputRequest); using (Stream webStream = getJobOutputResponse.Body) { using (Stream fileToSave = File.OpenWrite(fileName)) { CopyStream(webStream, fileToSave); } } } public static void CopyStream(Stream input, Stream output) { byte[] buffer = new byte[65536]; int length; while ((length = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, length); } } } }

Contoh 2: Mengambil Arsip Menggunakan API Tingkat Rendah dariAWS SDK for .NET—Unduh Output dalam Potongan

Contoh kode C# berikut mengambil arsip dari S3 Glacier. Contoh kode mengunduh output tugas dalam potongan dengan menentukan rentang byte dalam objek GetJobOutputRequest.

using System; using System.Collections.Generic; using System.IO; using System.Threading; using Amazon.Glacier; using Amazon.Glacier.Model; using Amazon.Glacier.Transfer; using Amazon.Runtime; using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; using Amazon.SQS; using Amazon.SQS.Model; using Newtonsoft.Json; using System.Collections.Specialized; namespace glacier.amazon.com.docsamples { class ArchiveDownloadLowLevelUsingSQLSNSOutputUsingRange { static string topicArn; static string queueUrl; static string queueArn; static string vaultName = "*** Provide vault name ***"; static string archiveId = "*** Provide archive ID ***"; static string fileName = "*** Provide the file name and path to where to store downloaded archive ***"; static AmazonSimpleNotificationServiceClient snsClient; static AmazonSQSClient sqsClient; const string SQS_POLICY = "{" + " \"Version\" : \"2012-10-17\"," + " \"Statement\" : [" + " {" + " \"Sid\" : \"sns-rule\"," + " \"Effect\" : \"Allow\"," + " \"Principal\" : {\"AWS\" : \"arn:aws:iam::123456789012:root\" }," + " \"Action\" : \"sqs:SendMessage\"," + " \"Resource\" : \"{QuernArn}\"," + " \"Condition\" : {" + " \"ArnLike\" : {" + " \"aws:SourceArn\" : \"{TopicArn}\"" + " }" + " }" + " }" + " ]" + "}"; public static void Main(string[] args) { AmazonGlacierClient client; try { using (client = new AmazonGlacierClient(Amazon.RegionEndpoint.USWest2)) { Console.WriteLine("Setup SNS topic and SQS queue."); SetupTopicAndQueue(); Console.WriteLine("To continue, press Enter"); Console.ReadKey(); Console.WriteLine("Download archive"); DownloadAnArchive(archiveId, client); } Console.WriteLine("Operations successful. To continue, press Enter"); Console.ReadKey(); } catch (AmazonGlacierException e) { Console.WriteLine(e.Message); } catch (AmazonServiceException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } finally { // Delete SNS topic and SQS queue. snsClient.DeleteTopic(new DeleteTopicRequest() { TopicArn = topicArn }); sqsClient.DeleteQueue(new DeleteQueueRequest() { QueueUrl = queueUrl }); } } static void SetupTopicAndQueue() { long ticks = DateTime.Now.Ticks; // Setup SNS topic. snsClient = new AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.USWest2); sqsClient = new AmazonSQSClient(Amazon.RegionEndpoint.USWest2); topicArn = snsClient.CreateTopic(new CreateTopicRequest { Name = "GlacierDownload-" + ticks }).TopicArn; Console.Write("topicArn: "); Console.WriteLine(topicArn); CreateQueueRequest createQueueRequest = new CreateQueueRequest(); createQueueRequest.QueueName = "GlacierDownload-" + ticks; CreateQueueResponse createQueueResponse = sqsClient.CreateQueue(createQueueRequest); queueUrl = createQueueResponse.QueueUrl; Console.Write("QueueURL: "); Console.WriteLine(queueUrl); GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(); getQueueAttributesRequest.AttributeNames = new List<string> { "QueueArn" }; getQueueAttributesRequest.QueueUrl = queueUrl; GetQueueAttributesResponse response = sqsClient.GetQueueAttributes(getQueueAttributesRequest); queueArn = response.QueueARN; Console.Write("QueueArn: "); Console.WriteLine(queueArn); // Setup the Amazon SNS topic to publish to the SQS queue. snsClient.Subscribe(new SubscribeRequest() { Protocol = "sqs", Endpoint = queueArn, TopicArn = topicArn }); // Add the policy to the queue so SNS can send messages to the queue. var policy = SQS_POLICY.Replace("{TopicArn}", topicArn).Replace("{QuernArn}", queueArn); sqsClient.SetQueueAttributes(new SetQueueAttributesRequest() { QueueUrl = queueUrl, Attributes = new Dictionary<string, string> { { QueueAttributeName.Policy, policy } } }); } static void DownloadAnArchive(string archiveId, AmazonGlacierClient client) { // Initiate job. InitiateJobRequest initJobRequest = new InitiateJobRequest() { VaultName = vaultName, JobParameters = new JobParameters() { Type = "archive-retrieval", ArchiveId = archiveId, Description = "This job is to download the archive.", SNSTopic = topicArn, } }; InitiateJobResponse initJobResponse = client.InitiateJob(initJobRequest); string jobId = initJobResponse.JobId; // Check queue for a message and if job completed successfully, download archive. ProcessQueue(jobId, client); } private static void ProcessQueue(string jobId, AmazonGlacierClient client) { var receiveMessageRequest = new ReceiveMessageRequest() { QueueUrl = queueUrl, MaxNumberOfMessages = 1 }; bool jobDone = false; while (!jobDone) { Console.WriteLine("Poll SQS queue"); ReceiveMessageResponse receiveMessageResponse = sqsClient.ReceiveMessage(receiveMessageRequest); if (receiveMessageResponse.Messages.Count == 0) { Thread.Sleep(10000 * 60); continue; } Console.WriteLine("Got message"); Message message = receiveMessageResponse.Messages[0]; Dictionary<string, string> outerLayer = JsonConvert.DeserializeObject<Dictionary<string, string>>(message.Body); Dictionary<string, object> fields = JsonConvert.DeserializeObject<Dictionary<string, object>>(outerLayer["Message"]); string statusCode = fields["StatusCode"] as string; if (string.Equals(statusCode, GlacierUtils.JOB_STATUS_SUCCEEDED, StringComparison.InvariantCultureIgnoreCase)) { long archiveSize = Convert.ToInt64(fields["ArchiveSizeInBytes"]); Console.WriteLine("Downloading job output"); DownloadOutput(jobId, archiveSize, client); // This where we save job output to the specified file location. } else if (string.Equals(statusCode, GlacierUtils.JOB_STATUS_FAILED, StringComparison.InvariantCultureIgnoreCase)) Console.WriteLine("Job failed... cannot download the archive."); jobDone = true; sqsClient.DeleteMessage(new DeleteMessageRequest() { QueueUrl = queueUrl, ReceiptHandle = message.ReceiptHandle }); } } private static void DownloadOutput(string jobId, long archiveSize, AmazonGlacierClient client) { long partSize = 4 * (long)Math.Pow(2, 20); // 4 MB. using (Stream fileToSave = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { long currentPosition = 0; do { GetJobOutputRequest getJobOutputRequest = new GetJobOutputRequest() { JobId = jobId, VaultName = vaultName }; long endPosition = currentPosition + partSize - 1; if (endPosition > archiveSize) endPosition = archiveSize; getJobOutputRequest.SetRange(currentPosition, endPosition); GetJobOutputResponse getJobOutputResponse = client.GetJobOutput(getJobOutputRequest); using (Stream webStream = getJobOutputResponse.Body) { CopyStream(webStream, fileToSave); } currentPosition += partSize; } while (currentPosition < archiveSize); } } public static void CopyStream(Stream input, Stream output) { byte[] buffer = new byte[65536]; int length; while ((length = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, length); } } } }