Amazon Glacier: Upload Archive from Memory
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 buffers data in memory.
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 PureBasic Downloads
IncludeFile "CkAuthAws.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkSocket.pb"
IncludeFile "CkCrypt2.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkBinData.pb"
Procedure ChilkatExample()
; Assumes Chilkat is unlocked. See Global Unlock Sample.
success.i = 0
; Private configuration, not a file in public web content or source control.
; Load current IAM credentials authorized for the Glacier operation.
credentials.i = CkJsonObject::ckCreate()
If credentials.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkStrict(credentials, 1)
success = CkJsonObject::ckLoadFile(credentials,"qa_data/aws/glacier.json")
If success <> 1
Debug "Could not load private AWS credentials."
CkJsonObject::ckDispose(credentials)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(credentials,"access_key") <> 1) OR (CkJsonObject::ckStringOf(credentials,"access_key") = "")
Debug "Missing or invalid access_key."
CkJsonObject::ckDispose(credentials)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(credentials,"secret_key") <> 1) OR (CkJsonObject::ckStringOf(credentials,"secret_key") = "")
Debug "Missing or invalid secret_key."
CkJsonObject::ckDispose(credentials)
ProcedureReturn
EndIf
; Set this once to the region containing the vault.
region.s = "us-west-2"
endpoint.i = CkStringBuilder::ckCreate()
If endpoint.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(endpoint,"glacier.")
CkStringBuilder::ckAppend(endpoint,region)
CkStringBuilder::ckAppend(endpoint,".amazonaws.com")
aws.i = CkAuthAws::ckCreate()
If aws.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkAuthAws::setCkAccessKey(aws, CkJsonObject::ckStringOf(credentials,"access_key"))
CkAuthAws::setCkSecretKey(aws, CkJsonObject::ckStringOf(credentials,"secret_key"))
CkAuthAws::setCkRegion(aws, region)
CkAuthAws::setCkServiceName(aws, "glacier")
CkAuthAws::setCkSignatureVersion(aws, 4)
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkRest::setCkConnectTimeoutMs(rest, 20000)
CkRest::setCkIdleTimeoutMs(rest, 60000)
success = CkRest::ckSetAuthAws(rest,aws)
If success <> 1
Debug "Could not configure AWS signing."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
; 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 CkJsonObject::ckHasMember(credentials,"session_token") = 1
If CkJsonObject::ckJsonTypeOf(credentials,"session_token") <> 1
Debug "session_token must be a string."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
If CkJsonObject::ckStringOf(credentials,"session_token") <> ""
success = CkRest::ckAddQueryParam(rest,"X-Amz-Security-Token",CkJsonObject::ckStringOf(credentials,"session_token"))
If success <> 1
Debug "Could not set the session-token parameter."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
EndIf
EndIf
; Supply a Socket to explicitly enable TLS certificate verification.
connection.i = CkSocket::ckCreate()
If connection.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkSocket::setCkRequireSslCertVerify(connection, 1)
CkSocket::setCkMaxReadIdleMs(connection, 60000)
CkSocket::setCkMaxSendIdleMs(connection, 60000)
success = CkSocket::ckConnect(connection,CkStringBuilder::ckGetAsString(endpoint),443,1,20000)
If success <> 1
Debug "TLS connection failed; inspect diagnostics privately."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
ProcedureReturn
EndIf
success = CkRest::ckUseConnection(rest,connection,1)
If success <> 1
Debug "Could not attach the TLS connection."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
ProcedureReturn
EndIf
success = CkRest::ckAddHeader(rest,"x-amz-glacier-version","2012-06-01")
If success <> 1
Debug "Could not set x-amz-glacier-version."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
ProcedureReturn
EndIf
; A hyphen selects the account belonging to the signing credentials.
; Use the original, URL-safe vault name and IDs returned by Glacier.
success = CkRest::ckAddPathParam(rest,"{vault}","chilkat")
If success <> 1
Debug "Could not set vault name."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
ProcedureReturn
EndIf
body.i = CkJsonObject::ckCreate()
If body.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
result.i = CkJsonObject::ckCreate()
If result.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkStrict(result, 1)
error.i = CkJsonObject::ckCreate()
If error.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkStrict(error, 1)
requestBody.i = CkStringBuilder::ckCreate()
If requestBody.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
responseBody.i = CkStringBuilder::ckCreate()
If responseBody.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
data.i = CkBinData::ckCreate()
If data.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkBinData::ckLoadFile(data,"qa_data/jpg/penguins.jpg")
If success <> 1
Debug "Could not load archive bytes."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
ProcedureReturn
EndIf
; The BinData can instead be populated directly by your application.
crypt.i = CkCrypt2::ckCreate()
If crypt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkCrypt2::setCkHashAlgorithm(crypt, "sha256-tree-hash")
CkCrypt2::setCkEncodingMode(crypt, "hexlower")
If CkBinData::ckNumBytes(data) = 0
CkCrypt2::setCkHashAlgorithm(crypt, "sha256")
EndIf
treeHash.s = CkCrypt2::ckHashBdENC(crypt,data)
If CkCrypt2::ckLastMethodSuccess(crypt) <> 1
Debug "Could not compute tree hash."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
CkCrypt2::ckDispose(crypt)
ProcedureReturn
EndIf
success = CkRest::ckAddHeader(rest,"x-amz-sha256-tree-hash",treeHash)
If success <> 1
Debug "Could not set x-amz-sha256-tree-hash."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
CkCrypt2::ckDispose(crypt)
ProcedureReturn
EndIf
CkCrypt2::setCkHashAlgorithm(crypt, "sha256")
linearHash.s = CkCrypt2::ckHashBdENC(crypt,data)
If CkCrypt2::ckLastMethodSuccess(crypt) <> 1
Debug "Could not compute payload hash."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
CkCrypt2::ckDispose(crypt)
ProcedureReturn
EndIf
CkAuthAws::setCkPrecomputedSha256(aws, linearHash)
; Set the precomputed hash before attaching the signing configuration again.
success = CkRest::ckSetAuthAws(rest,aws)
If success <> 1
Debug "Could not update AWS signing."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
CkCrypt2::ckDispose(crypt)
ProcedureReturn
EndIf
success = CkRest::ckAddHeader(rest,"x-amz-content-sha256",linearHash)
If success <> 1
Debug "Could not set x-amz-content-sha256."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
CkCrypt2::ckDispose(crypt)
ProcedureReturn
EndIf
success = CkRest::ckAddHeader(rest,"x-amz-archive-description","Example archive")
If success <> 1
Debug "Could not set x-amz-archive-description."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
CkCrypt2::ckDispose(crypt)
ProcedureReturn
EndIf
success = CkRest::ckFullRequestBd(rest,"POST","/-/vaults/{vault}/archives",data,responseBody)
If success <> 1
Debug "Upload did not complete; check for an accepted archive before retrying."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
CkCrypt2::ckDispose(crypt)
ProcedureReturn
EndIf
If CkRest::ckResponseStatusCode(rest) <> 201
Debug "Glacier HTTP status: " + Str(CkRest::ckResponseStatusCode(rest))
success = CkJsonObject::ckLoadSb(error,responseBody)
If success = 1
If CkJsonObject::ckJsonTypeOf(error,"code") = 1
Debug "AWS error code: " + CkJsonObject::ckStringOf(error,"code")
EndIf
EndIf
Debug "AWS request ID: " + CkRest::ckResponseHdrByName(rest,"x-amzn-RequestId")
; Inspect full errors privately. An HTTP error is not a successful operation.
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
CkCrypt2::ckDispose(crypt)
ProcedureReturn
EndIf
archiveId.s = CkRest::ckResponseHdrByName(rest,"x-amz-archive-id")
If (CkRest::ckLastMethodSuccess(rest) <> 1) OR (archiveId = "")
Debug "Missing archive ID; investigate the accepted upload before retrying."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
CkCrypt2::ckDispose(crypt)
ProcedureReturn
EndIf
; Save this ID even if subsequent checksum verification fails.
Debug "Archive ID: " + archiveId
Debug "Location: " + CkRest::ckResponseHdrByName(rest,"Location")
returnedHash.s = CkRest::ckResponseHdrByName(rest,"x-amz-sha256-tree-hash")
If returnedHash <> treeHash
Debug "Upload response checksum mismatch; preserve the archive ID and investigate."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
CkCrypt2::ckDispose(crypt)
ProcedureReturn
EndIf
Debug "Archive upload verified. Persist its ID and checksum in your archive catalog."
CkJsonObject::ckDispose(credentials)
CkStringBuilder::ckDispose(endpoint)
CkAuthAws::ckDispose(aws)
CkRest::ckDispose(rest)
CkSocket::ckDispose(connection)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(error)
CkStringBuilder::ckDispose(requestBody)
CkStringBuilder::ckDispose(responseBody)
CkBinData::ckDispose(data)
CkCrypt2::ckDispose(crypt)
ProcedureReturn
EndProcedure