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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoCredentials
String sRegion
Handle hoEndpoint
Variant vAws
Handle hoAws
Handle hoRest
Variant vConnection
Handle hoConnection
Handle hoBody
Handle hoResult
Handle hoError
Handle hoRequestBody
Variant vResponseBody
Handle hoResponseBody
String sFilePath
Handle hoCrypt
Handle hoFiles
String sFileSize
String sTreeHash
String sLinearHash
Variant vFileStream
Handle hoFileStream
String sResponse
String sArchiveId
String sReturnedHash
String sTemp1
Integer iTemp1
Boolean bTemp1
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Move False To iSuccess
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the Glacier operation.
Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
If (Not(IsComObjectCreated(hoCredentials))) Begin
Send CreateComObject of hoCredentials
End
Set ComStrict Of hoCredentials To True
Get ComLoadFile Of hoCredentials "qa_data/aws/glacier.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private AWS credentials."
Procedure_Return
End
Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
Get ComStringOf Of hoCredentials "access_key" To sTemp1
If ((iTemp1 <> 1) Or (sTemp1 = "")) Begin
Showln "Missing or invalid access_key."
Procedure_Return
End
Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
If ((iTemp1 <> 1) Or (sTemp1 = "")) Begin
Showln "Missing or invalid secret_key."
Procedure_Return
End
// Set this once to the region containing the vault.
Move "us-west-2" To sRegion
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "glacier." To iSuccess
Get ComAppend Of hoEndpoint sRegion To iSuccess
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
Get Create (RefClass(cComChilkatAuthAws)) To hoAws
If (Not(IsComObjectCreated(hoAws))) Begin
Send CreateComObject of hoAws
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Set ComAccessKey Of hoAws To sTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Set ComSecretKey Of hoAws To sTemp1
Set ComRegion Of hoAws To sRegion
Set ComServiceName Of hoAws To "glacier"
Set ComSignatureVersion Of hoAws To 4
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Set ComConnectTimeoutMs Of hoRest To 20000
Set ComIdleTimeoutMs Of hoRest To 60000
Get pvComObject of hoAws to vAws
Get ComSetAuthAws Of hoRest vAws To iSuccess
If (iSuccess <> True) Begin
Showln "Could not configure AWS signing."
Procedure_Return
End
// 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.
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
If (iTemp1 <> 1) Begin
Showln "session_token must be a string."
Procedure_Return
End
Get ComStringOf Of hoCredentials "session_token" To sTemp1
If (sTemp1 <> "") Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set the session-token parameter."
Procedure_Return
End
End
End
// Supply a Socket to explicitly enable TLS certificate verification.
Get Create (RefClass(cComChilkatSocket)) To hoConnection
If (Not(IsComObjectCreated(hoConnection))) Begin
Send CreateComObject of hoConnection
End
Set ComRequireSslCertVerify Of hoConnection To True
Set ComMaxReadIdleMs Of hoConnection To 60000
Set ComMaxSendIdleMs Of hoConnection To 60000
Get ComGetAsString Of hoEndpoint To sTemp1
Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
If (iSuccess <> True) Begin
Showln "TLS connection failed; inspect diagnostics privately."
Procedure_Return
End
Get pvComObject of hoConnection to vConnection
Get ComUseConnection Of hoRest vConnection True To iSuccess
If (iSuccess <> True) Begin
Showln "Could not attach the TLS connection."
Procedure_Return
End
Get ComAddHeader Of hoRest "x-amz-glacier-version" "2012-06-01" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set x-amz-glacier-version."
Procedure_Return
End
// A hyphen selects the account belonging to the signing credentials.
// Use the original, URL-safe vault name and IDs returned by Glacier.
Get ComAddPathParam Of hoRest "{vault}" "chilkat" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set vault name."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoBody
If (Not(IsComObjectCreated(hoBody))) Begin
Send CreateComObject of hoBody
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Set ComStrict Of hoResult To True
Get Create (RefClass(cComChilkatJsonObject)) To hoError
If (Not(IsComObjectCreated(hoError))) Begin
Send CreateComObject of hoError
End
Set ComStrict Of hoError To True
Get Create (RefClass(cComChilkatStringBuilder)) To hoRequestBody
If (Not(IsComObjectCreated(hoRequestBody))) Begin
Send CreateComObject of hoRequestBody
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoResponseBody
If (Not(IsComObjectCreated(hoResponseBody))) Begin
Send CreateComObject of hoResponseBody
End
Move "qa_data/zips/somethingBig.zip" To sFilePath
// Keep this file unchanged during BOTH hashing passes and the upload.
Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
If (Not(IsComObjectCreated(hoCrypt))) Begin
Send CreateComObject of hoCrypt
End
Set ComHashAlgorithm Of hoCrypt To "sha256-tree-hash"
Set ComEncodingMode Of hoCrypt To "hexlower"
Get Create (RefClass(cComCkFileAccess)) To hoFiles
If (Not(IsComObjectCreated(hoFiles))) Begin
Send CreateComObject of hoFiles
End
Get ComFileSizeStr Of hoFiles sFilePath To sFileSize
Get ComLastMethodSuccess Of hoFiles To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not read archive file size."
Procedure_Return
End
If (sFileSize = "0") Begin
Set ComHashAlgorithm Of hoCrypt To "sha256"
End
Get ComHashFileENC Of hoCrypt sFilePath To sTreeHash
Get ComLastMethodSuccess Of hoCrypt To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not compute tree hash."
Procedure_Return
End
Get ComAddHeader Of hoRest "x-amz-sha256-tree-hash" sTreeHash To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set x-amz-sha256-tree-hash."
Procedure_Return
End
Set ComHashAlgorithm Of hoCrypt To "sha256"
Get ComHashFileENC Of hoCrypt sFilePath To sLinearHash
Get ComLastMethodSuccess Of hoCrypt To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not compute payload hash."
Procedure_Return
End
Set ComPrecomputedSha256 Of hoAws To sLinearHash
// Set the precomputed hash before attaching the signing configuration again.
Get pvComObject of hoAws to vAws
Get ComSetAuthAws Of hoRest vAws To iSuccess
If (iSuccess <> True) Begin
Showln "Could not update AWS signing."
Procedure_Return
End
Get ComAddHeader Of hoRest "x-amz-content-sha256" sLinearHash To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set x-amz-content-sha256."
Procedure_Return
End
Get ComAddHeader Of hoRest "x-amz-archive-description" "Example archive" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set x-amz-archive-description."
Procedure_Return
End
Get Create (RefClass(cComChilkatStream)) To hoFileStream
If (Not(IsComObjectCreated(hoFileStream))) Begin
Send CreateComObject of hoFileStream
End
Set ComSourceFile Of hoFileStream To sFilePath
Set ComStreamNonChunked Of hoRest To True
Set ComIdleTimeoutMs Of hoRest To 120000
Set ComMaxReadIdleMs Of hoConnection To 120000
Set ComMaxSendIdleMs Of hoConnection To 120000
Get pvComObject of hoFileStream to vFileStream
Get ComFullRequestStream Of hoRest "POST" "/-/vaults/{vault}/archives" vFileStream To sResponse
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Showln "Upload did not complete; check for an accepted archive before retrying."
Procedure_Return
End
Get ComAppend Of hoResponseBody sResponse To iSuccess
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 201) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "Glacier HTTP status: " iTemp1
Get pvComObject of hoResponseBody to vResponseBody
Get ComLoadSb Of hoError vResponseBody To iSuccess
If (iSuccess = True) Begin
Get ComJsonTypeOf Of hoError "code" To iTemp1
If (iTemp1 = 1) Begin
Get ComStringOf Of hoError "code" To sTemp1
Showln "AWS error code: " sTemp1
End
End
Get ComResponseHdrByName Of hoRest "x-amzn-RequestId" To sTemp1
Showln "AWS request ID: " sTemp1
// Inspect full errors privately. An HTTP error is not a successful operation.
Procedure_Return
End
Get ComResponseHdrByName Of hoRest "x-amz-archive-id" To sArchiveId
Get ComLastMethodSuccess Of hoRest To bTemp1
If ((bTemp1 <> True) Or (sArchiveId = "")) Begin
Showln "Missing archive ID; investigate the accepted upload before retrying."
Procedure_Return
End
// Save this ID even if subsequent checksum verification fails.
Showln "Archive ID: " sArchiveId
Get ComResponseHdrByName Of hoRest "Location" To sTemp1
Showln "Location: " sTemp1
Get ComResponseHdrByName Of hoRest "x-amz-sha256-tree-hash" To sReturnedHash
If (sReturnedHash <> sTreeHash) Begin
Showln "Upload response checksum mismatch; preserve the archive ID and investigate."
Procedure_Return
End
Showln "Archive upload verified. Persist its ID and checksum in your archive catalog."
End_Procedure