Amazon Glacier: Download and Verify Range Retrieval Output
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.
Downloads the 8 MiB output of the companion range retrieval job. Set its job ID and create qa_output first. DescribeJob must report successful archive retrieval and the expected archive range. The HTTP range bytes=0-8388607 is relative to the job output, although the original archive range starts at 8388608.
Requires HTTP 206, exact Content-Range and Content-Length, the response tree hash, the actual downloaded size, and the job output's SHA256TreeHash before renaming the temporary file. ArchiveSHA256TreeHash hashes the entire original archive and must not be used to validate a partial retrieval. Existing output files are not overwritten. A failed download remains marked .part.
Requires glacier:DescribeJob and glacier:GetJobOutput. Download promptly: job output remains available for at least 24 hours after completion. Both retrieval and download ranges must be tree-hash aligned to receive a checksum. For other range lengths, adjust all expected ranges and lengths together.
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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
' Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
' Private configuration, not a file in public web content or source control.
' Load current IAM credentials authorized for the Glacier operation.
set credentials = Server.CreateObject("Chilkat.JsonObject")
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/glacier.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load private AWS credentials.") & "</pre>"
Response.End
End If
If ((credentials.JsonTypeOf("access_key") <> 1) Or (credentials.StringOf("access_key") = "")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid access_key.") & "</pre>"
Response.End
End If
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (credentials.StringOf("secret_key") = "")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid secret_key.") & "</pre>"
Response.End
End If
' Set this once to the region containing the vault.
region = "us-west-2"
set endpoint = Server.CreateObject("Chilkat.StringBuilder")
success = endpoint.Append("glacier.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
set aws = Server.CreateObject("Chilkat.AuthAws")
aws.AccessKey = credentials.StringOf("access_key")
aws.SecretKey = credentials.StringOf("secret_key")
aws.Region = region
aws.ServiceName = "glacier"
aws.SignatureVersion = 4
set rest = Server.CreateObject("Chilkat.Rest")
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
success = rest.SetAuthAws(aws)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not configure AWS signing.") & "</pre>"
Response.End
End If
' 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 (credentials.HasMember("session_token") = 1) Then
If (credentials.JsonTypeOf("session_token") <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "session_token must be a string.") & "</pre>"
Response.End
End If
If (credentials.StringOf("session_token") <> "") Then
success = rest.AddQueryParam("X-Amz-Security-Token",credentials.StringOf("session_token"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set the session-token parameter.") & "</pre>"
Response.End
End If
End If
End If
' Supply a Socket to explicitly enable TLS certificate verification.
set connection = Server.CreateObject("Chilkat.Socket")
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "TLS connection failed; inspect diagnostics privately.") & "</pre>"
Response.End
End If
success = rest.UseConnection(connection,1)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not attach the TLS connection.") & "</pre>"
Response.End
End If
success = rest.AddHeader("x-amz-glacier-version","2012-06-01")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set x-amz-glacier-version.") & "</pre>"
Response.End
End If
' A hyphen selects the account belonging to the signing credentials.
' Use the original, URL-safe vault name and IDs returned by Glacier.
success = rest.AddPathParam("{vault}","chilkat")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set vault name.") & "</pre>"
Response.End
End If
set result = Server.CreateObject("Chilkat.JsonObject")
result.Strict = 1
set error = Server.CreateObject("Chilkat.JsonObject")
error.Strict = 1
set responseBody = Server.CreateObject("Chilkat.StringBuilder")
jobId = "YOUR_JOB_ID"
success = rest.AddPathParam("{job}",jobId)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set job ID.") & "</pre>"
Response.End
End If
success = rest.FullRequestNoBodySb("GET","/-/vaults/{vault}/jobs/{job}",responseBody)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Request did not complete; inspect diagnostics privately. Check state before retrying a change.") & "</pre>"
Response.End
End If
If (rest.ResponseStatusCode <> 200) Then
Response.Write "<pre>" & Server.HTMLEncode( "Glacier HTTP status: " & rest.ResponseStatusCode) & "</pre>"
success = error.LoadSb(responseBody)
If (success = 1) Then
If (error.JsonTypeOf("code") = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "AWS error code: " & error.StringOf("code")) & "</pre>"
End If
End If
Response.Write "<pre>" & Server.HTMLEncode( "AWS request ID: " & rest.ResponseHdrByName("x-amzn-RequestId")) & "</pre>"
' Inspect full errors privately. An HTTP error is not a successful operation.
Response.End
End If
success = result.LoadSb(responseBody)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected valid JSON response.") & "</pre>"
Response.End
End If
If (result.JsonTypeOf("Completed") <> 5) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid Completed.") & "</pre>"
Response.End
End If
If (result.JsonTypeOf("StatusCode") <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid StatusCode.") & "</pre>"
Response.End
End If
If (result.StringOf("StatusCode") = "") Then
Response.Write "<pre>" & Server.HTMLEncode( "Empty StatusCode.") & "</pre>"
Response.End
End If
If ((result.BoolOf("Completed") <> 1) Or (result.StringOf("StatusCode") <> "Succeeded")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Job is not successfully complete. Resume later only if InProgress.") & "</pre>"
Response.End
End If
If (result.StringOf("Action") <> "ArchiveRetrieval") Then
Response.Write "<pre>" & Server.HTMLEncode( "Wrong job type.") & "</pre>"
Response.End
End If
If (result.JsonTypeOf("SHA256TreeHash") <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid SHA256TreeHash.") & "</pre>"
Response.End
End If
If (result.StringOf("SHA256TreeHash") = "") Then
Response.Write "<pre>" & Server.HTMLEncode( "Empty SHA256TreeHash.") & "</pre>"
Response.End
End If
If (result.StringOf("RetrievalByteRange") <> "8388608-16777215") Then
Response.Write "<pre>" & Server.HTMLEncode( "Use the 8 MiB range job created by the companion example.") & "</pre>"
Response.End
End If
' These offsets are relative to the JOB OUTPUT, not the original archive.
' Output bytes 0..8388607 correspond to archive bytes 8388608..16777215.
expectedHash = result.StringOf("SHA256TreeHash")
set rangeHeader = Server.CreateObject("Chilkat.StringBuilder")
set contentRange = Server.CreateObject("Chilkat.StringBuilder")
set byteCount = Server.CreateObject("Chilkat.StringBuilder")
success = rangeHeader.Append("bytes=0-8388607")
success = contentRange.Append("bytes 0-8388607/8388608")
success = byteCount.Append("8388608")
set files = Server.CreateObject("Chilkat.FileAccess")
set crypt = Server.CreateObject("Chilkat.Crypt2")
crypt.HashAlgorithm = "sha256-tree-hash"
crypt.EncodingMode = "hexlower"
temporaryPath = "qa_output/archive-range.dat.part"
finalPath = "qa_output/archive-range.dat"
If ((files.FileExists(temporaryPath) = 1) Or (files.FileExists(finalPath) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Choose unused output paths.") & "</pre>"
Response.End
End If
success = rest.AddHeader("Range",rangeHeader.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set Range.") & "</pre>"
Response.End
End If
success = rest.SendReqNoBody("GET","/-/vaults/{vault}/jobs/{job}/output")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not send range request. Rerun to resume.") & "</pre>"
Response.End
End If
httpStatus = rest.ReadResponseHeader()
If (httpStatus <> 206) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected HTTP 206 for range download. Job may have expired; no new retrieval was started.") & "</pre>"
Response.End
End If
responseRange = rest.ResponseHdrByName("Content-Range")
If ((rest.LastMethodSuccess <> 1) Or (responseRange <> contentRange.GetAsString())) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected Content-Range; refusing download.") & "</pre>"
Response.End
End If
responseLength = rest.ResponseHdrByName("Content-Length")
If ((rest.LastMethodSuccess <> 1) Or (responseLength <> byteCount.GetAsString())) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected Content-Length; refusing download.") & "</pre>"
Response.End
End If
responseHash = rest.ResponseHdrByName("x-amz-sha256-tree-hash")
If ((rest.LastMethodSuccess <> 1) Or (responseHash = "")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing range tree hash. Require a tree-hash-aligned retrieval and download range.") & "</pre>"
Response.End
End If
set output = Server.CreateObject("Chilkat.Stream")
output.SinkFile = temporaryPath
success = rest.ReadRespBodyStream(output,1)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Download interrupted; rerun to resume. Temporary bytes are not trusted.") & "</pre>"
Response.End
End If
actualSize = files.FileSizeStr(temporaryPath)
If ((files.LastMethodSuccess <> 1) Or (actualSize <> byteCount.GetAsString())) Then
Response.Write "<pre>" & Server.HTMLEncode( "Downloaded size mismatch.") & "</pre>"
Response.End
End If
actualHash = crypt.HashFileENC(temporaryPath)
If ((crypt.LastMethodSuccess <> 1) Or (actualHash <> responseHash)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Downloaded tree hash mismatch.") & "</pre>"
Response.End
End If
If (actualHash <> expectedHash) Then
Response.Write "<pre>" & Server.HTMLEncode( "Range output does not match the retrieval job tree hash.") & "</pre>"
Response.End
End If
success = files.FileRename(temporaryPath,finalPath)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Verified range remains at the temporary path; rename failed.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Archive range downloaded and verified.") & "</pre>"
%>
</body>
</html>