DataFlex
DataFlex
REST Asynchronous Streaming Upload File
See more REST Examples
Demonstrates how to asynchronous streaming upload a file to cloud storage. This particular example demonstrates an upload to the Azure Cloud Storage service. The same concepts apply to S3, Google Cloud, and Google Drive.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoRest
Boolean iBTls
Integer iPort
Boolean iBAutoReconnect
Variant vAzAuth
Handle hoAzAuth
Variant vSendStream
Handle hoSendStream
Variant vTask
Handle hoTask
Integer iCurPctDone
Integer iResponseStatusCode
String sResponseBodyStr
String sTemp1
Integer iTemp1
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 Azure Storage Blob Service
Move True To iBTls
Move 443 To iPort
Move True To iBAutoReconnect
// In this example, the storage account name is "chilkat".
Get ComConnect Of hoRest "chilkat.blob.core.windows.net" iPort iBTls iBAutoReconnect To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// Provide Azure Cloud credentials for the REST call.
Get Create (RefClass(cComChilkatAuthAzureStorage)) To hoAzAuth
If (Not(IsComObjectCreated(hoAzAuth))) Begin
Send CreateComObject of hoAzAuth
End
Set ComAccessKey Of hoAzAuth To "AZURE_ACCESS_KEY"
// The account name used here should match the 1st part of the domain passed in the call to Connect (above).
Set ComAccount Of hoAzAuth To "chilkat"
Set ComScheme Of hoAzAuth To "SharedKey"
Set ComService Of hoAzAuth To "Blob"
// This causes the "x-ms-version: 2021-08-06" header to be automatically added.
Set ComXMsVersion Of hoAzAuth To "2021-08-06"
Get pvComObject of hoAzAuth to vAzAuth
Get ComSetAuthAzureStorage Of hoRest vAzAuth To iSuccess
// Set some request headers.
Get ComAddHeader Of hoRest "x-ms-blob-content-disposition" 'attachment; filename="thisIsATest.txt"' To iSuccess
Get ComAddHeader Of hoRest "x-ms-blob-type" "BlockBlob" To iSuccess
Get ComAddHeader Of hoRest "x-ms-meta-m1" "v1" To iSuccess
Get ComAddHeader Of hoRest "x-ms-meta-m2" "v2" To iSuccess
// Note: The application does not need to explicitly set the following
// headers: x-ms-date, Authorization, and Content-Length. These headers
// are automatically set by Chilkat.
Get Create (RefClass(cComChilkatStream)) To hoSendStream
If (Not(IsComObjectCreated(hoSendStream))) Begin
Send CreateComObject of hoSendStream
End
// Define the source data for the stream to be a file.
Set ComSourceFile Of hoSendStream To "qa_data/hamlet.xml"
// Create a background thread task to upload from the stream
// The name of the Azure storage container is "test".
Get pvComObject of hoSendStream to vSendStream
Get ComFullRequestStreamAsync Of hoRest "PUT" "/test/thisIsATest.txt" vSendStream To vTask
If (IsComObject(vTask)) Begin
Get Create (RefClass(cComChilkatTask)) To hoTask
Set pvComObject Of hoTask To vTask
End
// Start the task.
Get ComRun Of hoTask To iSuccess
// In this example, we'll simply sleep and periodically
// check to see if the REST upload if finished.
Move 0 To iCurPctDone
While ((ComFinished(hoTask)) <> True)
Send ComSleepMs To hoTask 100
Loop
// Check to see if the call to FullRequestStream in the background thread pool succeeded.
Get ComTaskSuccess Of hoTask To bTemp1
If (bTemp1 <> True) Begin
// Show what would've been the LastErrorText had FullRequestStream been called synchronously
Get ComResultErrorText Of hoTask To sTemp1
Showln sTemp1
Send Destroy of hoTask
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iResponseStatusCode
// When successful, the Azure Storage service will respond with a 201 response code,
// with an empty body. Therefore, in the success condition, the responseStr is empty.
If (iResponseStatusCode = 201) Begin
Showln "File uploaded."
End
Else Begin
// It failed, so examine the response body, if one was returned:
// Given that FullRequestStream returns a string, the return value is obtained via GetResultString.
Get ComGetResultString Of hoTask To sResponseBodyStr
Showln "response body (if any): " sResponseBodyStr
// Examine the request/response to see what happened.
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "response status code = " iTemp1
Get ComResponseStatusText Of hoRest To sTemp1
Showln "response status text = " sTemp1
Get ComResponseHeader Of hoRest To sTemp1
Showln "response header: " sTemp1
Showln "---"
Get ComLastRequestStartLine Of hoRest To sTemp1
Showln "LastRequestStartLine: " sTemp1
Get ComLastRequestHeader Of hoRest To sTemp1
Showln "LastRequestHeader: " sTemp1
End
Send Destroy of hoTask
End_Procedure