Amazon Glacier: Resume a Multipart Upload
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.
Uses fixed 8 MiB parts, at most 10,000 parts (78.125 GiB), and buffers one part at a time. The API also allows other power-of-two MiB sizes up to 4 GiB; this portable example deliberately fixes the size. The last part may be smaller. Use a nonempty, immutable file and one writer per upload. Create qa_output first. Byte offsets and archive sizes stay exact in languages with 32-bit integers.
The private checkpoint stores the upload ID, region, vault, file path, size, part size, and full-file tree hash. Preserve it across failures. It contains no AWS credentials. A separate completion receipt records the archive ID. A lost initiation response requires inspecting List Multipart Uploads; starting a new upload is not a retry of the old one. Inactive uploads can expire after 24 hours. Do not abort automatically on transient errors.
Verifies the local file against its checkpoint, reads every List Parts page, and compares each exact range and tree hash. Matching parts are skipped; missing or mismatched parts are uploaded. Unexpected server ranges prevent completion. Keep the upload exclusive to this process. A missing upload may be completed, aborted, or expired: do not automatically start over. For an uncertain completion use retry completion.
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 body As New ChilkatJsonObject
Dim result As New ChilkatJsonObject
result.Strict = 1
Dim error As New ChilkatJsonObject
error.Strict = 1
Dim requestBody As New ChilkatStringBuilder
Dim responseBody As New ChilkatStringBuilder
Dim files As New CkFileAccess
Dim statePath As String
statePath = "qa_output/glacier-multipart.json"
Dim receiptPath As String
receiptPath = "qa_output/glacier-multipart-completed.json"
Dim state As New ChilkatJsonObject
state.Strict = 1
Dim saved As New ChilkatStringBuilder
success = state.LoadFile(statePath)
If (success <> 1) Then
Debug.Print "Could not load the private multipart checkpoint."
Exit Sub
End If
If (state.JsonTypeOf("UploadId") <> 1) Then
Debug.Print "Missing or invalid UploadId."
Exit Sub
End If
If (state.StringOf("UploadId") = "") Then
Debug.Print "Empty UploadId."
Exit Sub
End If
If (state.JsonTypeOf("Region") <> 1) Then
Debug.Print "Missing or invalid Region."
Exit Sub
End If
If (state.StringOf("Region") = "") Then
Debug.Print "Empty Region."
Exit Sub
End If
If (state.JsonTypeOf("VaultName") <> 1) Then
Debug.Print "Missing or invalid VaultName."
Exit Sub
End If
If (state.StringOf("VaultName") = "") Then
Debug.Print "Empty VaultName."
Exit Sub
End If
If (state.JsonTypeOf("FilePath") <> 1) Then
Debug.Print "Missing or invalid FilePath."
Exit Sub
End If
If (state.StringOf("FilePath") = "") Then
Debug.Print "Empty FilePath."
Exit Sub
End If
If (state.JsonTypeOf("ArchiveSize") <> 1) Then
Debug.Print "Missing or invalid ArchiveSize."
Exit Sub
End If
If (state.StringOf("ArchiveSize") = "") Then
Debug.Print "Empty ArchiveSize."
Exit Sub
End If
If (state.JsonTypeOf("TreeHash") <> 1) Then
Debug.Print "Missing or invalid TreeHash."
Exit Sub
End If
If (state.StringOf("TreeHash") = "") Then
Debug.Print "Empty TreeHash."
Exit Sub
End If
If (state.JsonTypeOf("PartSize") <> 1) Then
Debug.Print "Missing or invalid PartSize."
Exit Sub
End If
If (state.StringOf("PartSize") = "") Then
Debug.Print "Empty PartSize."
Exit Sub
End If
If ((state.StringOf("Region") <> region) Or (state.StringOf("VaultName") <> "chilkat")) Then
Debug.Print "Checkpoint region/vault differs from this example configuration."
Exit Sub
End If
If (state.StringOf("PartSize") <> "8388608") Then
Debug.Print "This workflow requires the original 8 MiB part size."
Exit Sub
End If
Dim uploadId As String
uploadId = state.StringOf("UploadId")
success = rest.AddPathParam("{upload}",uploadId)
If (success <> 1) Then
Debug.Print "Could not set upload ID."
Exit Sub
End If
If (files.FileExists(receiptPath) = 1) Then
Debug.Print "A completion receipt already exists; this upload has finished."
Exit Sub
End If
Dim filePath As String
filePath = state.StringOf("FilePath")
Dim fileSize As String
fileSize = files.FileSizeStr(filePath)
If ((files.LastMethodSuccess <> 1) Or (fileSize <> state.StringOf("ArchiveSize"))) Then
Debug.Print "Local file size differs from the checkpoint."
Exit Sub
End If
Dim crypt As New ChilkatCrypt2
crypt.HashAlgorithm = "sha256-tree-hash"
crypt.EncodingMode = "hexlower"
Dim treeHash As String
treeHash = crypt.HashFileENC(filePath)
If ((crypt.LastMethodSuccess <> 1) Or (treeHash <> state.StringOf("TreeHash"))) Then
Debug.Print "Local file checksum differs from the checkpoint; do not resume with a different file."
Exit Sub
End If
Dim remoteParts As New ChilkatHashtable
success = rest.AddQueryParam("limit","50")
If (success <> 1) Then
Debug.Print "Could not set page limit."
Exit Sub
End If
Dim seenMarkers As New ChilkatHashtable
Dim marker As String
marker = ""
Dim more As Long
more = 1
Dim page As Long
page = 0
Do While (more = 1)
If (page = 1000) Then
Debug.Print "Page limit reached; listing is incomplete."
Exit Sub
End If
page = page + 1
If (marker <> "") Then
success = rest.AddQueryParam("marker",marker)
If (success <> 1) Then
Debug.Print "Could not set marker."
Exit Sub
End If
End If
success = rest.FullRequestNoBodySb("GET","/-/vaults/{vault}/multipart-uploads/{upload}",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("MultipartUploadId") <> 1) Then
Debug.Print "Missing or invalid MultipartUploadId."
Exit Sub
End If
If (result.StringOf("MultipartUploadId") = "") Then
Debug.Print "Empty MultipartUploadId."
Exit Sub
End If
If (result.JsonTypeOf("PartSizeInBytes") <> 2) Then
Debug.Print "Missing or invalid PartSizeInBytes."
Exit Sub
End If
If ((result.StringOf("MultipartUploadId") <> uploadId) Or (result.StringOf("PartSizeInBytes") <> state.StringOf("PartSize"))) Then
Debug.Print "Server upload identity or part size differs from checkpoint."
Exit Sub
End If
If (result.JsonTypeOf("Parts") <> 4) Then
Debug.Print "Missing or invalid Parts."
Exit Sub
End If
Dim i As Long
i = 0
Do While (i < result.SizeOfArray("Parts"))
result.I = i
If (result.JsonTypeOf("Parts[i].RangeInBytes") <> 1) Then
Debug.Print "Missing or invalid Parts[i].RangeInBytes."
Exit Sub
End If
If (result.StringOf("Parts[i].RangeInBytes") = "") Then
Debug.Print "Empty Parts[i].RangeInBytes."
Exit Sub
End If
If (result.JsonTypeOf("Parts[i].SHA256TreeHash") <> 1) Then
Debug.Print "Missing or invalid Parts[i].SHA256TreeHash."
Exit Sub
End If
If (result.StringOf("Parts[i].SHA256TreeHash") = "") Then
Debug.Print "Empty Parts[i].SHA256TreeHash."
Exit Sub
End If
Dim remoteRange As String
remoteRange = result.StringOf("Parts[i].RangeInBytes")
If (remoteParts.Contains(remoteRange) = 1) Then
Debug.Print "Duplicate server range; listing is inconsistent."
Exit Sub
End If
success = remoteParts.AddStr(remoteRange,result.StringOf("Parts[i].SHA256TreeHash"))
If (success <> 1) Then
Debug.Print "Could not record server part."
Exit Sub
End If
i = i + 1
Loop
more = 0
If (result.JsonTypeOf("Marker") = 1) Then
marker = result.StringOf("Marker")
If (marker = "") Then
Debug.Print "Empty marker; listing is incomplete."
Exit Sub
End If
If (seenMarkers.Contains(marker) = 1) Then
Debug.Print "Repeated marker; listing is incomplete."
Exit Sub
End If
success = seenMarkers.AddStr(marker,"seen")
If (success <> 1) Then
Debug.Print "Could not track pagination."
Exit Sub
End If
more = 1
Else
If (result.JsonTypeOf("Marker") <> 6) Then
Debug.Print "Missing or invalid Marker; listing is incomplete."
Exit Sub
End If
End If
Loop
success = rest.RemoveQueryParam("limit")
If (success <> 1) Then
Debug.Print "Could not clear limit."
Exit Sub
End If
If (marker <> "") Then
success = rest.RemoveQueryParam("marker")
If (success <> 1) Then
Debug.Print "Could not clear marker."
Exit Sub
End If
End If
success = files.OpenForRead(filePath)
If (success <> 1) Then
Debug.Print "Could not open the immutable source file."
files.FileClose
Exit Sub
End If
Dim partSize As Long
partSize = 8388608
Dim numParts As Long
numParts = files.GetNumBlocks(partSize)
If ((numParts < 1) Or (numParts > 10000)) Then
Debug.Print "This 8 MiB example supports nonempty files of at most 10,000 parts."
files.FileClose
Exit Sub
End If
' Two base-1,000,000,000 components avoid 32-bit overflow in COM languages.
Dim byteHigh As Long
byteHigh = 0
Dim byteLow As Long
byteLow = 0
Dim endHigh As Long
Dim endLow As Long
Dim range As New ChilkatStringBuilder
Dim digits As New ChilkatStringBuilder
Dim part As New ChilkatBinData
Dim partIndex As Long
partIndex = 0
Dim matchedRemoteParts As Long
matchedRemoteParts = 0
Do While (partIndex < numParts)
success = part.Clear()
If (success <> 1) Then
Debug.Print "Could not clear part buffer."
files.FileClose
Exit Sub
End If
success = files.ReadBlockBd(partIndex,partSize,part)
If (success <> 1) Then
Debug.Print "Could not read the source part."
files.FileClose
Exit Sub
End If
If ((part.NumBytes = 0) Or ((partIndex < numParts - 1) And (part.NumBytes <> partSize))) Then
Debug.Print "Source file changed or a part was read incompletely."
files.FileClose
Exit Sub
End If
range.Clear
digits.Clear
success = digits.AppendInt(byteLow)
If (byteHigh > 0) Then
Do While (digits.Length < 9)
success = digits.Prepend("0")
Loop
success = range.AppendInt(byteHigh)
End If
success = range.Append(digits.GetAsString())
success = range.Append("-")
endHigh = byteHigh
endLow = byteLow + part.NumBytes - 1
If (endLow >= 1000000000) Then
endLow = endLow - 1000000000
endHigh = endHigh + 1
End If
digits.Clear
success = digits.AppendInt(endLow)
If (endHigh > 0) Then
Do While (digits.Length < 9)
success = digits.Prepend("0")
Loop
success = range.AppendInt(endHigh)
End If
success = range.Append(digits.GetAsString())
Dim rangeKey As String
rangeKey = range.GetAsString()
crypt.HashAlgorithm = "sha256-tree-hash"
Dim partHash As String
partHash = crypt.HashBdENC(part)
If (crypt.LastMethodSuccess <> 1) Then
Debug.Print "Could not hash source part."
files.FileClose
Exit Sub
End If
Dim sendPart As Long
sendPart = 1
If (remoteParts.Contains(rangeKey) = 1) Then
matchedRemoteParts = matchedRemoteParts + 1
If (remoteParts.LookupStr(rangeKey) = partHash) Then
sendPart = 0
Debug.Print "Verified existing part: " & rangeKey
End If
End If
If (sendPart = 1) Then
crypt.HashAlgorithm = "sha256"
Dim linearHash As String
linearHash = crypt.HashBdENC(part)
If (crypt.LastMethodSuccess <> 1) Then
Debug.Print "Could not hash the part payload."
files.FileClose
Exit Sub
End If
aws.PrecomputedSha256 = linearHash
success = rest.SetAuthAws(aws)
If (success <> 1) Then
Debug.Print "Could not configure part signing."
files.FileClose
Exit Sub
End If
success = rest.AddHeader("x-amz-content-sha256",linearHash)
If (success <> 1) Then
Debug.Print "Could not set x-amz-content-sha256."
files.FileClose
Exit Sub
End If
success = rest.AddHeader("x-amz-sha256-tree-hash",partHash)
If (success <> 1) Then
Debug.Print "Could not set x-amz-sha256-tree-hash."
files.FileClose
Exit Sub
End If
success = range.Prepend("bytes ")
success = range.Append("/*")
success = rest.AddHeader("Content-Range",range.GetAsString())
If (success <> 1) Then
Debug.Print "Could not set Content-Range."
files.FileClose
Exit Sub
End If
success = rest.AddHeader("Content-Type","application/octet-stream")
If (success <> 1) Then
Debug.Print "Could not set Content-Type."
files.FileClose
Exit Sub
End If
success = rest.FullRequestBd("PUT","/-/vaults/{vault}/multipart-uploads/{upload}",part,responseBody)
If (success <> 1) Then
Debug.Print "Part request did not complete. Keep the checkpoint and run the resume example."
files.FileClose
Exit Sub
End If
If (rest.ResponseStatusCode <> 204) 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.
files.FileClose
Exit Sub
End If
Dim confirmedHash As String
confirmedHash = rest.ResponseHdrByName("x-amz-sha256-tree-hash")
If (confirmedHash <> partHash) Then
Debug.Print "Part response checksum mismatch. Keep the checkpoint and inspect List Parts."
files.FileClose
Exit Sub
End If
Debug.Print "Uploaded part: " & rangeKey
End If
byteLow = byteLow + part.NumBytes
If (byteLow >= 1000000000) Then
byteLow = byteLow - 1000000000
byteHigh = byteHigh + 1
End If
partIndex = partIndex + 1
Loop
files.FileClose
If (matchedRemoteParts <> remoteParts.Count) Then
Debug.Print "Server contains unexpected ranges. Do not complete; inspect the upload and source."
files.FileClose
Exit Sub
End If
' Recheck the source before completion. Keep the file immutable throughout.
fileSize = files.FileSizeStr(filePath)
If ((files.LastMethodSuccess <> 1) Or (fileSize <> state.StringOf("ArchiveSize"))) Then
Debug.Print "Source size changed during upload; do not complete."
files.FileClose
Exit Sub
End If
crypt.HashAlgorithm = "sha256-tree-hash"
treeHash = crypt.HashFileENC(filePath)
If ((crypt.LastMethodSuccess <> 1) Or (treeHash <> state.StringOf("TreeHash"))) Then
Debug.Print "Source checksum changed during upload; do not complete."
files.FileClose
Exit Sub
End If
success = rest.AddHeader("x-amz-sha256-tree-hash",state.StringOf("TreeHash"))
If (success <> 1) Then
Debug.Print "Could not set x-amz-sha256-tree-hash."
files.FileClose
Exit Sub
End If
success = rest.AddHeader("x-amz-archive-size",state.StringOf("ArchiveSize"))
If (success <> 1) Then
Debug.Print "Could not set x-amz-archive-size."
files.FileClose
Exit Sub
End If
' Completion has NO body: never reuse a part's precomputed payload hash.
aws.PrecomputedSha256 = ""
success = rest.SetAuthAws(aws)
If (success <> 1) Then
Debug.Print "Could not reset signing."
files.FileClose
Exit Sub
End If
success = rest.RemoveHeader("x-amz-content-sha256")
If (success <> 1) Then
Debug.Print "Could not clear part payload hash."
files.FileClose
Exit Sub
End If
success = rest.RemoveHeader("Content-Range")
If (success <> 1) Then
Debug.Print "Could not clear part range."
files.FileClose
Exit Sub
End If
success = rest.RemoveHeader("Content-Type")
If (success <> 1) Then
Debug.Print "Could not clear content type."
files.FileClose
Exit Sub
End If
success = rest.FullRequestNoBodySb("POST","/-/vaults/{vault}/multipart-uploads/{upload}",responseBody)
If (success <> 1) Then
Debug.Print "Request did not complete; inspect diagnostics privately. Check state before retrying a change."
files.FileClose
Exit Sub
End If
If (rest.ResponseStatusCode <> 201) 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.
files.FileClose
Exit Sub
End If
Dim archiveId As String
archiveId = rest.ResponseHdrByName("x-amz-archive-id")
If ((rest.LastMethodSuccess <> 1) Or (archiveId = "")) Then
Debug.Print "Completion response has no archive ID. Preserve the checkpoint and retry identical completion promptly."
files.FileClose
Exit Sub
End If
Debug.Print "Archive ID: " & archiveId
Dim location As String
location = rest.ResponseHdrByName("Location")
Debug.Print "Location: " & location
Dim returnedHash As String
returnedHash = rest.ResponseHdrByName("x-amz-sha256-tree-hash")
If ((rest.LastMethodSuccess = 1) And (returnedHash <> state.StringOf("TreeHash"))) Then
Debug.Print "Completion checksum mismatch; preserve the printed archive ID and investigate."
files.FileClose
Exit Sub
End If
Dim receipt As New ChilkatJsonObject
success = receipt.UpdateString("UploadId",uploadId)
If (success <> 1) Then
Debug.Print "Could not set UploadId."
files.FileClose
Exit Sub
End If
success = receipt.UpdateString("ArchiveId",archiveId)
If (success <> 1) Then
Debug.Print "Could not set ArchiveId."
files.FileClose
Exit Sub
End If
success = receipt.UpdateString("Location",location)
If (success <> 1) Then
Debug.Print "Could not set Location."
files.FileClose
Exit Sub
End If
success = receipt.UpdateString("TreeHash",state.StringOf("TreeHash"))
If (success <> 1) Then
Debug.Print "Could not set TreeHash."
files.FileClose
Exit Sub
End If
saved.Clear
success = receipt.EmitSb(saved)
If (success <> 1) Then
Debug.Print "Could not serialize receipt; preserve the printed archive ID."
files.FileClose
Exit Sub
End If
success = saved.WriteFile(receiptPath,"utf-8",0)
If (success <> 1) Then
Debug.Print "Could not save receipt; preserve the printed archive ID."
files.FileClose
Exit Sub
End If
Debug.Print "Completion receipt saved. Keep it in your archive catalog."