DataFlex
DataFlex
Glacier Download Archive
See more Amazon Glacier Examples
Demonstrates how to download an Amazon Glacier archive. Downloading a Glacier archive is a two step process. First an archive retrieval job is initiated. After it completes, the job output is downloaded (i.e. the archive contents are downloaded.)Note: This example requires Chilkat v9.5.0.78 or greater.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoRest
Boolean iBTls
Integer iPort
Boolean iBAutoReconnect
Variant vAuthAws
Handle hoAuthAws
Integer iResponseStatusCode
Variant vBodyStream
Handle hoBodyStream
String sJsonError
String sTemp1
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
// Connect to the Amazon AWS REST server in the desired region.
Move True To iBTls
Move 443 To iPort
Move True To iBAutoReconnect
Get ComConnect Of hoRest "glacier.us-west-2.amazonaws.com" iPort iBTls iBAutoReconnect To iSuccess
// Provide AWS credentials.
Get Create (RefClass(cComChilkatAuthAws)) To hoAuthAws
If (Not(IsComObjectCreated(hoAuthAws))) Begin
Send CreateComObject of hoAuthAws
End
Set ComAccessKey Of hoAuthAws To "AWS_ACCESS_KEY"
Set ComSecretKey Of hoAuthAws To "AWS_SECRET_KEY"
Set ComServiceName Of hoAuthAws To "glacier"
Set ComRegion Of hoAuthAws To "us-west-2"
Get pvComObject of hoAuthAws to vAuthAws
Get ComSetAuthAws Of hoRest vAuthAws To iSuccess
// --------------------------------------------------------------------------
// Note: The above REST connection and setup of the AWS credentials
// can be done once. After connecting, any number of REST calls can be made.
// The "auto reconnect" property passed to rest.Connect indicates that if
// the connection is lost, a REST method call will automatically reconnect
// if needed.
// --------------------------------------------------------------------------
//
// For more information, see Glacier Retrieve Job Output Reference Documentation
//
Get ComAddHeader Of hoRest "x-amz-glacier-version" "2012-06-01" To iSuccess
// To download an archive, we'll make several method calls:
// 1) Send the HTTPS GET request.
// 2) Get the response body.
// 3) If the response status code indicates success, stream the response body (i.e the archive data) to a file.
// 4) If the response status code indicates an error, get the JSON error response.
// Send the GET request.
Get ComSendReqNoBody Of hoRest "GET" "/AWS_ACCOUNT_ID/vaults/chilkat/jobs/JOB_ID/output" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// Read the response header.
Get ComReadResponseHeader Of hoRest To iResponseStatusCode
If (iResponseStatusCode < 0) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Response status code = " iResponseStatusCode
// We expect a 200 response status if the archive data is coming.
// Otherwise, we'll get a JSON response body with an error message(or no response body).
If (iResponseStatusCode = 200) Begin
Get Create (RefClass(cComChilkatStream)) To hoBodyStream
If (Not(IsComObjectCreated(hoBodyStream))) Begin
Send CreateComObject of hoBodyStream
End
// The stream's sink will be a file.
Set ComSinkFile Of hoBodyStream To "qa_output/archiveData.dat"
// Read the response body to the stream.
Get pvComObject of hoBodyStream to vBodyStream
Get ComReadRespBodyStream Of hoRest vBodyStream True To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Successfully received the archive file."
End
Else Begin
Get ComReadRespBodyString Of hoRest To sJsonError
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
End
Else Begin
Showln sJsonError
End
End
End_Procedure