Amazon Glacier: Initiate an Archive Range Retrieval
See more Amazon Glacier Examples
For existing Amazon Glacier vault customers. The standalone vault service is closed to new customers. These examples use its REST API version 2012-06-01, not the S3 bucket APIs used by S3 Glacier storage classes. See AWS service guidance.
Starts a Bulk retrieval of archive bytes 8388608 through 16777215, inclusive (8 MiB). The archive must contain at least 16 MiB. Edit the archive ID before running. The JSON RetrievalByteRange contains archive offsets without bytes=. Its start must be a multiple of 1 MiB; its end plus one must be a multiple of 1 MiB, or its end must be the archive's last byte.
This example also chooses a tree-hash-aligned range: a power-of-two number of MiB starting on that size's boundary. Merely being MiB-aligned does not guarantee a returned tree hash. Save the returned job ID and wait for successful completion using DescribeJob or SNS, then use download range output. Retrieval can incur charges. A failed or timed-out response does not prove that a job was not created; list jobs before retrying.
Requires glacier:InitiateJob. Bulk is appropriate for nonurgent retrieval; this example does not poll or initiate repeated jobs.
Uses AWS Signature Version 4 with service glacier. Set the region once; the example builds the commercial AWS endpoint. Other partitions require the appropriate endpoint suffix. /-/ selects the signing account; change the vault name and IDs to your own resources. Load private qa_data/aws/glacier.json with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This sample does not discover profiles or refresh credentials. TLS certificate verification is enabled explicitly.
Checks transport completion, the operation-specific HTTP status, and parsed data separately. Inspect full diagnostics privately. Renew expired credentials; use bounded backoff with jitter for retryable reads. Uploads and job initiation can succeed before a connection fails, so do not blindly resubmit them. Configure IAM permissions for only the required operations and vault resources.
Chilkat C Downloads
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>
#include <C_CkAuthAws.h>
#include <C_CkRest.h>
#include <C_CkSocket.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObject credentials;
const char *region;
HCkStringBuilder endpoint;
HCkAuthAws aws;
HCkRest rest;
HCkSocket connection;
HCkJsonObject body;
HCkJsonObject result;
HCkJsonObject error;
HCkStringBuilder requestBody;
HCkStringBuilder responseBody;
const char *jobId;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = FALSE;
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the Glacier operation.
credentials = CkJsonObject_Create();
CkJsonObject_putStrict(credentials,TRUE);
success = CkJsonObject_LoadFile(credentials,"qa_data/aws/glacier.json");
if (success != TRUE) {
printf("Could not load private AWS credentials.\n");
CkJsonObject_Dispose(credentials);
return;
}
if ((CkJsonObject_JsonTypeOf(credentials,"access_key") != 1) || (CkJsonObject_stringOf(credentials,"access_key") == "")) {
printf("Missing or invalid access_key.\n");
CkJsonObject_Dispose(credentials);
return;
}
if ((CkJsonObject_JsonTypeOf(credentials,"secret_key") != 1) || (CkJsonObject_stringOf(credentials,"secret_key") == "")) {
printf("Missing or invalid secret_key.\n");
CkJsonObject_Dispose(credentials);
return;
}
// Set this once to the region containing the vault.
region = "us-west-2";
endpoint = CkStringBuilder_Create();
CkStringBuilder_Append(endpoint,"glacier.");
CkStringBuilder_Append(endpoint,region);
CkStringBuilder_Append(endpoint,".amazonaws.com");
aws = CkAuthAws_Create();
CkAuthAws_putAccessKey(aws,CkJsonObject_stringOf(credentials,"access_key"));
CkAuthAws_putSecretKey(aws,CkJsonObject_stringOf(credentials,"secret_key"));
CkAuthAws_putRegion(aws,region);
CkAuthAws_putServiceName(aws,"glacier");
CkAuthAws_putSignatureVersion(aws,4);
rest = CkRest_Create();
CkRest_putConnectTimeoutMs(rest,20000);
CkRest_putIdleTimeoutMs(rest,60000);
success = CkRest_SetAuthAws(rest,aws);
if (success != TRUE) {
printf("Could not configure AWS signing.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
return;
}
// Temporary IAM/STS credentials require their session token on every signed request.
// Include the token in the signed query. Keep request URLs and diagnostics private.
// Leave session_token absent or empty only for credentials that do not have one.
if (CkJsonObject_HasMember(credentials,"session_token") == TRUE) {
if (CkJsonObject_JsonTypeOf(credentials,"session_token") != 1) {
printf("session_token must be a string.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
return;
}
if (CkJsonObject_stringOf(credentials,"session_token") != "") {
success = CkRest_AddQueryParam(rest,"X-Amz-Security-Token",CkJsonObject_stringOf(credentials,"session_token"));
if (success != TRUE) {
printf("Could not set the session-token parameter.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
return;
}
}
}
// Supply a Socket to explicitly enable TLS certificate verification.
connection = CkSocket_Create();
CkSocket_putRequireSslCertVerify(connection,TRUE);
CkSocket_putMaxReadIdleMs(connection,60000);
CkSocket_putMaxSendIdleMs(connection,60000);
success = CkSocket_Connect(connection,CkStringBuilder_getAsString(endpoint),443,TRUE,20000);
if (success != TRUE) {
printf("TLS connection failed; inspect diagnostics privately.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
return;
}
success = CkRest_UseConnection(rest,connection,TRUE);
if (success != TRUE) {
printf("Could not attach the TLS connection.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
return;
}
success = CkRest_AddHeader(rest,"x-amz-glacier-version","2012-06-01");
if (success != TRUE) {
printf("Could not set x-amz-glacier-version.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
return;
}
// A hyphen selects the account belonging to the signing credentials.
// Use the original, URL-safe vault name and IDs returned by Glacier.
success = CkRest_AddPathParam(rest,"{vault}","chilkat");
if (success != TRUE) {
printf("Could not set vault name.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
return;
}
body = CkJsonObject_Create();
result = CkJsonObject_Create();
CkJsonObject_putStrict(result,TRUE);
error = CkJsonObject_Create();
CkJsonObject_putStrict(error,TRUE);
requestBody = CkStringBuilder_Create();
responseBody = CkStringBuilder_Create();
// Retrieve the second 8 MiB of an archive of at least 16 MiB.
// Inclusive archive offsets; do NOT prefix this JSON value with "bytes=".
success = CkJsonObject_UpdateString(body,"Type","archive-retrieval");
if (success != TRUE) {
printf("Could not set Type.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(error);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);
return;
}
success = CkJsonObject_UpdateString(body,"ArchiveId","YOUR_ARCHIVE_ID");
if (success != TRUE) {
printf("Could not set ArchiveId.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(error);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);
return;
}
success = CkJsonObject_UpdateString(body,"Tier","Bulk");
if (success != TRUE) {
printf("Could not set Tier.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(error);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);
return;
}
success = CkJsonObject_UpdateString(body,"RetrievalByteRange","8388608-16777215");
if (success != TRUE) {
printf("Could not set RetrievalByteRange.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(error);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);
return;
}
success = CkRest_AddHeader(rest,"Content-Type","application/json");
if (success != TRUE) {
printf("Could not set Content-Type.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(error);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);
return;
}
success = CkJsonObject_EmitSb(body,requestBody);
if (success != TRUE) {
printf("Could not encode JSON request.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(error);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);
return;
}
success = CkRest_FullRequestSb(rest,"POST","/-/vaults/{vault}/jobs",requestBody,responseBody);
if (success != TRUE) {
printf("Request did not complete; inspect diagnostics privately. Check state before retrying a change.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(error);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);
return;
}
if (CkRest_getResponseStatusCode(rest) != 201) {
printf("Glacier HTTP status: %d\n",CkRest_getResponseStatusCode(rest));
success = CkJsonObject_LoadSb(error,responseBody);
if (success == TRUE) {
if (CkJsonObject_JsonTypeOf(error,"code") == 1) {
printf("AWS error code: %s\n",CkJsonObject_stringOf(error,"code"));
}
}
printf("AWS request ID: %s\n",CkRest_responseHdrByName(rest,"x-amzn-RequestId"));
// Inspect full errors privately. An HTTP error is not a successful operation.
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(error);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);
return;
}
jobId = CkRest_responseHdrByName(rest,"x-amz-job-id");
if ((CkRest_getLastMethodSuccess(rest) != TRUE) || (jobId == "")) {
printf("Job may have started, but no job ID was received. List jobs before submitting again.\n");
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(error);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);
return;
}
printf("Range retrieval job ID: %s\n",jobId);
printf("Save this ID and wait for Completed=true and StatusCode=Succeeded.\n");
// A timeout can leave an accepted job. InitiateJob has no idempotency token.
// Do not blindly resubmit: retrieval requests can incur charges.
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(error);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);
}