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 Visual Basic 6.0 Downloads
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim success As Long
success = 0
' Private configuration, not a file in public web content or source control.
' Load current IAM credentials authorized for the Glacier operation.
Dim credentials As New ChilkatJsonObject
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/glacier.json")
If (success <> 1) Then
Debug.Print "Could not load private AWS credentials."
Exit Sub
End If
If ((credentials.JsonTypeOf("access_key") <> 1) Or (credentials.StringOf("access_key") = "")) Then
Debug.Print "Missing or invalid access_key."
Exit Sub
End If
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (credentials.StringOf("secret_key") = "")) Then
Debug.Print "Missing or invalid secret_key."
Exit Sub
End If
' Set this once to the region containing the vault.
Dim region As String
region = "us-west-2"
Dim endpoint As New ChilkatStringBuilder
success = endpoint.Append("glacier.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
Dim aws As New ChilkatAuthAws
aws.AccessKey = credentials.StringOf("access_key")
aws.SecretKey = credentials.StringOf("secret_key")
aws.Region = region
aws.ServiceName = "glacier"
aws.SignatureVersion = 4
Dim rest As New ChilkatRest
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
success = rest.SetAuthAws(aws)
If (success <> 1) Then
Debug.Print "Could not configure AWS signing."
Exit Sub
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
Debug.Print "session_token must be a string."
Exit Sub
End If
If (credentials.StringOf("session_token") <> "") Then
success = rest.AddQueryParam("X-Amz-Security-Token",credentials.StringOf("session_token"))
If (success <> 1) Then
Debug.Print "Could not set the session-token parameter."
Exit Sub
End If
End If
End If
' Supply a Socket to explicitly enable TLS certificate verification.
Dim connection As New ChilkatSocket
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
Debug.Print "TLS connection failed; inspect diagnostics privately."
Exit Sub
End If
success = rest.UseConnection(connection,1)
If (success <> 1) Then
Debug.Print "Could not attach the TLS connection."
Exit Sub
End If
success = rest.AddHeader("x-amz-glacier-version","2012-06-01")
If (success <> 1) Then
Debug.Print "Could not set x-amz-glacier-version."
Exit Sub
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
Debug.Print "Could not set vault name."
Exit Sub
End If
Dim result As New ChilkatJsonObject
result.Strict = 1
Dim error As New ChilkatJsonObject
error.Strict = 1
Dim responseBody As New ChilkatStringBuilder
Dim jobId As String
jobId = "YOUR_JOB_ID"
success = rest.AddPathParam("{job}",jobId)
If (success <> 1) Then
Debug.Print "Could not set job ID."
Exit Sub
End If
success = rest.FullRequestNoBodySb("GET","/-/vaults/{vault}/jobs/{job}",responseBody)
If (success <> 1) Then
Debug.Print "Request did not complete; inspect diagnostics privately. Check state before retrying a change."
Exit Sub
End If
If (rest.ResponseStatusCode <> 200) Then
Debug.Print "Glacier HTTP status: " & rest.ResponseStatusCode
success = error.LoadSb(responseBody)
If (success = 1) Then
If (error.JsonTypeOf("code") = 1) Then
Debug.Print "AWS error code: " & error.StringOf("code")
End If
End If
Debug.Print "AWS request ID: " & rest.ResponseHdrByName("x-amzn-RequestId")
' Inspect full errors privately. An HTTP error is not a successful operation.
Exit Sub
End If
success = result.LoadSb(responseBody)
If (success <> 1) Then
Debug.Print "Expected valid JSON response."
Exit Sub
End If
If (result.JsonTypeOf("Completed") <> 5) Then
Debug.Print "Missing or invalid Completed."
Exit Sub
End If
If (result.JsonTypeOf("StatusCode") <> 1) Then
Debug.Print "Missing or invalid StatusCode."
Exit Sub
End If
If (result.StringOf("StatusCode") = "") Then
Debug.Print "Empty StatusCode."
Exit Sub
End If
If ((result.BoolOf("Completed") <> 1) Or (result.StringOf("StatusCode") <> "Succeeded")) Then
Debug.Print "Job is not successfully complete. Resume later only if InProgress."
Exit Sub
End If
If (result.StringOf("Action") <> "ArchiveRetrieval") Then
Debug.Print "Wrong job type."
Exit Sub
End If
If (result.JsonTypeOf("SHA256TreeHash") <> 1) Then
Debug.Print "Missing or invalid SHA256TreeHash."
Exit Sub
End If
If (result.StringOf("SHA256TreeHash") = "") Then
Debug.Print "Empty SHA256TreeHash."
Exit Sub
End If
If (result.StringOf("RetrievalByteRange") <> "8388608-16777215") Then
Debug.Print "Use the 8 MiB range job created by the companion example."
Exit Sub
End If
' These offsets are relative to the JOB OUTPUT, not the original archive.
' Output bytes 0..8388607 correspond to archive bytes 8388608..16777215.
Dim expectedHash As String
expectedHash = result.StringOf("SHA256TreeHash")
Dim rangeHeader As New ChilkatStringBuilder
Dim contentRange As New ChilkatStringBuilder
Dim byteCount As New ChilkatStringBuilder
success = rangeHeader.Append("bytes=0-8388607")
success = contentRange.Append("bytes 0-8388607/8388608")
success = byteCount.Append("8388608")
Dim files As New CkFileAccess
Dim crypt As New ChilkatCrypt2
crypt.HashAlgorithm = "sha256-tree-hash"
crypt.EncodingMode = "hexlower"
Dim temporaryPath As String
temporaryPath = "qa_output/archive-range.dat.part"
Dim finalPath As String
finalPath = "qa_output/archive-range.dat"
If ((files.FileExists(temporaryPath) = 1) Or (files.FileExists(finalPath) = 1)) Then
Debug.Print "Choose unused output paths."
Exit Sub
End If
success = rest.AddHeader("Range",rangeHeader.GetAsString())
If (success <> 1) Then
Debug.Print "Could not set Range."
Exit Sub
End If
success = rest.SendReqNoBody("GET","/-/vaults/{vault}/jobs/{job}/output")
If (success <> 1) Then
Debug.Print "Could not send range request. Rerun to resume."
Exit Sub
End If
Dim httpStatus As Long
httpStatus = rest.ReadResponseHeader()
If (httpStatus <> 206) Then
Debug.Print "Expected HTTP 206 for range download. Job may have expired; no new retrieval was started."
Exit Sub
End If
Dim responseRange As String
responseRange = rest.ResponseHdrByName("Content-Range")
If ((rest.LastMethodSuccess <> 1) Or (responseRange <> contentRange.GetAsString())) Then
Debug.Print "Unexpected Content-Range; refusing download."
Exit Sub
End If
Dim responseLength As String
responseLength = rest.ResponseHdrByName("Content-Length")
If ((rest.LastMethodSuccess <> 1) Or (responseLength <> byteCount.GetAsString())) Then
Debug.Print "Unexpected Content-Length; refusing download."
Exit Sub
End If
Dim responseHash As String
responseHash = rest.ResponseHdrByName("x-amz-sha256-tree-hash")
If ((rest.LastMethodSuccess <> 1) Or (responseHash = "")) Then
Debug.Print "Missing range tree hash. Require a tree-hash-aligned retrieval and download range."
Exit Sub
End If
Dim output As New ChilkatStream
output.SinkFile = temporaryPath
success = rest.ReadRespBodyStream(output,1)
If (success <> 1) Then
Debug.Print "Download interrupted; rerun to resume. Temporary bytes are not trusted."
Exit Sub
End If
Dim actualSize As String
actualSize = files.FileSizeStr(temporaryPath)
If ((files.LastMethodSuccess <> 1) Or (actualSize <> byteCount.GetAsString())) Then
Debug.Print "Downloaded size mismatch."
Exit Sub
End If
Dim actualHash As String
actualHash = crypt.HashFileENC(temporaryPath)
If ((crypt.LastMethodSuccess <> 1) Or (actualHash <> responseHash)) Then
Debug.Print "Downloaded tree hash mismatch."
Exit Sub
End If
If (actualHash <> expectedHash) Then
Debug.Print "Range output does not match the retrieval job tree hash."
Exit Sub
End If
success = files.FileRename(temporaryPath,finalPath)
If (success <> 1) Then
Debug.Print "Verified range remains at the temporary path; rename failed."
Exit Sub
End If
Debug.Print "Archive range downloaded and verified."