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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Credentials
string ls_Region
oleobject loo_Endpoint
oleobject loo_Aws
oleobject loo_Rest
oleobject loo_Connection
oleobject loo_Body
oleobject loo_Result
oleobject loo_Error
oleobject loo_RequestBody
oleobject loo_ResponseBody
string ls_FilePath
oleobject loo_Crypt
oleobject loo_Files
string ls_FileSize
string ls_TreeHash
string ls_LinearHash
oleobject loo_FileStream
string ls_Response
string ls_ArchiveId
string ls_ReturnedHash
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the Glacier operation.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Credentials
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/glacier.json")
if li_Success <> 1 then
Write-Debug "Could not load private AWS credentials."
destroy loo_Credentials
return
end if
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (loo_Credentials.StringOf("access_key") = "") then
Write-Debug "Missing or invalid access_key."
destroy loo_Credentials
return
end if
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (loo_Credentials.StringOf("secret_key") = "") then
Write-Debug "Missing or invalid secret_key."
destroy loo_Credentials
return
end if
// Set this once to the region containing the vault.
ls_Region = "us-west-2"
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")
loo_Endpoint.Append("glacier.")
loo_Endpoint.Append(ls_Region)
loo_Endpoint.Append(".amazonaws.com")
loo_Aws = create oleobject
li_rc = loo_Aws.ConnectToNewObject("Chilkat.AuthAws")
loo_Aws.AccessKey = loo_Credentials.StringOf("access_key")
loo_Aws.SecretKey = loo_Credentials.StringOf("secret_key")
loo_Aws.Region = ls_Region
loo_Aws.ServiceName = "glacier"
loo_Aws.SignatureVersion = 4
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
li_Success = loo_Rest.SetAuthAws(loo_Aws)
if li_Success <> 1 then
Write-Debug "Could not configure AWS signing."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
return
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 loo_Credentials.HasMember("session_token") = 1 then
if loo_Credentials.JsonTypeOf("session_token") <> 1 then
Write-Debug "session_token must be a string."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
return
end if
if loo_Credentials.StringOf("session_token") <> "" then
li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",loo_Credentials.StringOf("session_token"))
if li_Success <> 1 then
Write-Debug "Could not set the session-token parameter."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
return
end if
end if
end if
// Supply a Socket to explicitly enable TLS certificate verification.
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")
loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
Write-Debug "TLS connection failed; inspect diagnostics privately."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
li_Success = loo_Rest.UseConnection(loo_Connection,1)
if li_Success <> 1 then
Write-Debug "Could not attach the TLS connection."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
li_Success = loo_Rest.AddHeader("x-amz-glacier-version","2012-06-01")
if li_Success <> 1 then
Write-Debug "Could not set x-amz-glacier-version."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
// A hyphen selects the account belonging to the signing credentials.
// Use the original, URL-safe vault name and IDs returned by Glacier.
li_Success = loo_Rest.AddPathParam("{vault}","chilkat")
if li_Success <> 1 then
Write-Debug "Could not set vault name."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")
loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")
loo_Result.Strict = 1
loo_Error = create oleobject
li_rc = loo_Error.ConnectToNewObject("Chilkat.JsonObject")
loo_Error.Strict = 1
loo_RequestBody = create oleobject
li_rc = loo_RequestBody.ConnectToNewObject("Chilkat.StringBuilder")
loo_ResponseBody = create oleobject
li_rc = loo_ResponseBody.ConnectToNewObject("Chilkat.StringBuilder")
ls_FilePath = "qa_data/zips/somethingBig.zip"
// Keep this file unchanged during BOTH hashing passes and the upload.
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
loo_Crypt.HashAlgorithm = "sha256-tree-hash"
loo_Crypt.EncodingMode = "hexlower"
loo_Files = create oleobject
li_rc = loo_Files.ConnectToNewObject("Chilkat.FileAccess")
ls_FileSize = loo_Files.FileSizeStr(ls_FilePath)
if loo_Files.LastMethodSuccess <> 1 then
Write-Debug "Could not read archive file size."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
return
end if
if ls_FileSize = "0" then
loo_Crypt.HashAlgorithm = "sha256"
end if
ls_TreeHash = loo_Crypt.HashFileENC(ls_FilePath)
if loo_Crypt.LastMethodSuccess <> 1 then
Write-Debug "Could not compute tree hash."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
return
end if
li_Success = loo_Rest.AddHeader("x-amz-sha256-tree-hash",ls_TreeHash)
if li_Success <> 1 then
Write-Debug "Could not set x-amz-sha256-tree-hash."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
return
end if
loo_Crypt.HashAlgorithm = "sha256"
ls_LinearHash = loo_Crypt.HashFileENC(ls_FilePath)
if loo_Crypt.LastMethodSuccess <> 1 then
Write-Debug "Could not compute payload hash."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
return
end if
loo_Aws.PrecomputedSha256 = ls_LinearHash
// Set the precomputed hash before attaching the signing configuration again.
li_Success = loo_Rest.SetAuthAws(loo_Aws)
if li_Success <> 1 then
Write-Debug "Could not update AWS signing."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
return
end if
li_Success = loo_Rest.AddHeader("x-amz-content-sha256",ls_LinearHash)
if li_Success <> 1 then
Write-Debug "Could not set x-amz-content-sha256."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
return
end if
li_Success = loo_Rest.AddHeader("x-amz-archive-description","Example archive")
if li_Success <> 1 then
Write-Debug "Could not set x-amz-archive-description."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
return
end if
loo_FileStream = create oleobject
li_rc = loo_FileStream.ConnectToNewObject("Chilkat.Stream")
loo_FileStream.SourceFile = ls_FilePath
loo_Rest.StreamNonChunked = 1
loo_Rest.IdleTimeoutMs = 120000
loo_Connection.MaxReadIdleMs = 120000
loo_Connection.MaxSendIdleMs = 120000
ls_Response = loo_Rest.FullRequestStream("POST","/-/vaults/{vault}/archives",loo_FileStream)
if loo_Rest.LastMethodSuccess <> 1 then
Write-Debug "Upload did not complete; check for an accepted archive before retrying."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
destroy loo_FileStream
return
end if
loo_ResponseBody.Append(ls_Response)
if loo_Rest.ResponseStatusCode <> 201 then
Write-Debug "Glacier HTTP status: " + string(loo_Rest.ResponseStatusCode)
li_Success = loo_Error.LoadSb(loo_ResponseBody)
if li_Success = 1 then
if loo_Error.JsonTypeOf("code") = 1 then
Write-Debug "AWS error code: " + loo_Error.StringOf("code")
end if
end if
Write-Debug "AWS request ID: " + loo_Rest.ResponseHdrByName("x-amzn-RequestId")
// Inspect full errors privately. An HTTP error is not a successful operation.
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
destroy loo_FileStream
return
end if
ls_ArchiveId = loo_Rest.ResponseHdrByName("x-amz-archive-id")
if (loo_Rest.LastMethodSuccess <> 1) OR (ls_ArchiveId = "") then
Write-Debug "Missing archive ID; investigate the accepted upload before retrying."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
destroy loo_FileStream
return
end if
// Save this ID even if subsequent checksum verification fails.
Write-Debug "Archive ID: " + ls_ArchiveId
Write-Debug "Location: " + loo_Rest.ResponseHdrByName("Location")
ls_ReturnedHash = loo_Rest.ResponseHdrByName("x-amz-sha256-tree-hash")
if ls_ReturnedHash <> ls_TreeHash then
Write-Debug "Upload response checksum mismatch; preserve the archive ID and investigate."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
destroy loo_FileStream
return
end if
Write-Debug "Archive upload verified. Persist its ID and checksum in your archive catalog."
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Error
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Crypt
destroy loo_Files
destroy loo_FileStream