Amazon Glacier: Initiate an Archive Range Retrieval
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.
Starts a Bulk retrieval of archive bytes 8388608 through 16777215, inclusive (8 MiB). The archive must contain at least 16 MiB. Edit the archive ID before running. The JSON RetrievalByteRange contains archive offsets without bytes=. Its start must be a multiple of 1 MiB; its end plus one must be a multiple of 1 MiB, or its end must be the archive's last byte.
This example also chooses a tree-hash-aligned range: a power-of-two number of MiB starting on that size's boundary. Merely being MiB-aligned does not guarantee a returned tree hash. Save the returned job ID and wait for successful completion using DescribeJob or SNS, then use download range output. Retrieval can incur charges. A failed or timed-out response does not prove that a job was not created; list jobs before retrying.
Requires glacier:InitiateJob. Bulk is appropriate for nonurgent retrieval; this example does not poll or initiate repeated jobs.
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 "CkSocket.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkAuthAws.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
; Retrieve the second 8 MiB of an archive of at least 16 MiB.
; Inclusive archive offsets; do NOT prefix this JSON value with "bytes=".
success = CkJsonObject::ckUpdateString(body,"Type","archive-retrieval")
If success <> 1
Debug "Could not set Type."
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)
ProcedureReturn
EndIf
success = CkJsonObject::ckUpdateString(body,"ArchiveId","YOUR_ARCHIVE_ID")
If success <> 1
Debug "Could not set ArchiveId."
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)
ProcedureReturn
EndIf
success = CkJsonObject::ckUpdateString(body,"Tier","Bulk")
If success <> 1
Debug "Could not set Tier."
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)
ProcedureReturn
EndIf
success = CkJsonObject::ckUpdateString(body,"RetrievalByteRange","8388608-16777215")
If success <> 1
Debug "Could not set RetrievalByteRange."
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)
ProcedureReturn
EndIf
success = CkRest::ckAddHeader(rest,"Content-Type","application/json")
If success <> 1
Debug "Could not set Content-Type."
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)
ProcedureReturn
EndIf
success = CkJsonObject::ckEmitSb(body,requestBody)
If success <> 1
Debug "Could not encode JSON request."
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)
ProcedureReturn
EndIf
success = CkRest::ckFullRequestSb(rest,"POST","/-/vaults/{vault}/jobs",requestBody,responseBody)
If success <> 1
Debug "Request did not complete; inspect diagnostics privately. Check state before retrying a change."
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)
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)
ProcedureReturn
EndIf
jobId.s = CkRest::ckResponseHdrByName(rest,"x-amz-job-id")
If (CkRest::ckLastMethodSuccess(rest) <> 1) OR (jobId = "")
Debug "Job may have started, but no job ID was received. List jobs before submitting again."
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)
ProcedureReturn
EndIf
Debug "Range retrieval job ID: " + jobId
Debug "Save this ID and wait for Completed=true and StatusCode=Succeeded."
; A timeout can leave an accepted job. InitiateJob has no idempotency token.
; Do not blindly resubmit: retrieval requests can incur charges.
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)
ProcedureReturn
EndProcedure