DataFlex
DataFlex
(using AWS Signature Version 2) Streaming Download to File
See more Amazon S3 (new) Examples
The main purpose of this example is to demonstrate how to use the older Signature Version 2 authentication w/ S3. It uses V2 authentication to download a file.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 sErrResponse
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.
Move True To iBTls
Move 443 To iPort
Move True To iBAutoReconnect
// The file is located in the bucket named "chilkat100", which becomes part of the domain:
Get ComConnect Of hoRest "chilkat100.s3.amazonaws.com" iPort iBTls iBAutoReconnect To iSuccess
// Provide AWS credentials for the REST call.
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 "s3"
// For AWS Signature Version 2, the following two properties need to be set:
Set ComSignatureVersion Of hoAuthAws To 2
// See http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#ConstructingTheCanonicalizedResourceElement
Set ComCanonicalizedResourceV2 Of hoAuthAws To "/chilkat100/starfish.jpg"
Get pvComObject of hoAuthAws to vAuthAws
Get ComSetAuthAws Of hoRest vAuthAws To iSuccess
// Send the request to download the JPG.
Get ComSendReqNoBody Of hoRest "GET" "/starfish.jpg" 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 JPG data is coming.
// Otherwise, we'll get a string 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/starfish.jpg"
// Read the response body to the stream. Given that we've
// set the stream's sink to a file, it will stream directly
// to the file.
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 JPG file."
End
Else Begin
Get ComReadRespBodyString Of hoRest To sErrResponse
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
End
Else Begin
Showln sErrResponse
End
End
End_Procedure