Amazon Glacier: Upload Archive from a File
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.
Computes both SHA-256 hashes, uploads immutable archive bytes, checks HTTP 201 and the returned tree hash, and prints the archive ID for durable storage in your catalog. A description is optional, must be printable ASCII, and is limited to 1,024 characters; it is not a lookup key. Single-request uploads support up to 4 GiB; use multipart upload for larger archives. This variant hashes and streams a file without buffering the full upload; the file must remain unchanged.
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 filePath As String
filePath = "qa_data/zips/somethingBig.zip"
' Keep this file unchanged during BOTH hashing passes and the upload.
Dim crypt As New ChilkatCrypt2
crypt.HashAlgorithm = "sha256-tree-hash"
crypt.EncodingMode = "hexlower"
Dim files As New CkFileAccess
Dim fileSize As String
fileSize = files.FileSizeStr(filePath)
If (files.LastMethodSuccess <> 1) Then
Debug.Print "Could not read archive file size."
Exit Sub
End If
If (fileSize = "0") Then
crypt.HashAlgorithm = "sha256"
End If
Dim treeHash As String
treeHash = crypt.HashFileENC(filePath)
If (crypt.LastMethodSuccess <> 1) Then
Debug.Print "Could not compute tree hash."
Exit Sub
End If
success = rest.AddHeader("x-amz-sha256-tree-hash",treeHash)
If (success <> 1) Then
Debug.Print "Could not set x-amz-sha256-tree-hash."
Exit Sub
End If
crypt.HashAlgorithm = "sha256"
Dim linearHash As String
linearHash = crypt.HashFileENC(filePath)
If (crypt.LastMethodSuccess <> 1) Then
Debug.Print "Could not compute payload hash."
Exit Sub
End If
aws.PrecomputedSha256 = linearHash
' Set the precomputed hash before attaching the signing configuration again.
success = rest.SetAuthAws(aws)
If (success <> 1) Then
Debug.Print "Could not update AWS signing."
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."
Exit Sub
End If
success = rest.AddHeader("x-amz-archive-description","Example archive")
If (success <> 1) Then
Debug.Print "Could not set x-amz-archive-description."
Exit Sub
End If
Dim fileStream As New ChilkatStream
fileStream.SourceFile = filePath
rest.StreamNonChunked = 1
rest.IdleTimeoutMs = 120000
connection.MaxReadIdleMs = 120000
connection.MaxSendIdleMs = 120000
Dim response As String
response = rest.FullRequestStream("POST","/-/vaults/{vault}/archives",fileStream)
If (rest.LastMethodSuccess <> 1) Then
Debug.Print "Upload did not complete; check for an accepted archive before retrying."
Exit Sub
End If
success = responseBody.Append(response)
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.
Exit Sub
End If
Dim archiveId As String
archiveId = rest.ResponseHdrByName("x-amz-archive-id")
If ((rest.LastMethodSuccess <> 1) Or (archiveId = "")) Then
Debug.Print "Missing archive ID; investigate the accepted upload before retrying."
Exit Sub
End If
' Save this ID even if subsequent checksum verification fails.
Debug.Print "Archive ID: " & archiveId
Debug.Print "Location: " & rest.ResponseHdrByName("Location")
Dim returnedHash As String
returnedHash = rest.ResponseHdrByName("x-amz-sha256-tree-hash")
If (returnedHash <> treeHash) Then
Debug.Print "Upload response checksum mismatch; preserve the archive ID and investigate."
Exit Sub
End If
Debug.Print "Archive upload verified. Persist its ID and checksum in your archive catalog."