DataFlex
DataFlex
Upload File in Blocks and Commit the Block List
See more Azure Cloud Storage Examples
Demonstrates how to upload a file in blocks and then commit the block list. For files larger than 64MB, this is the way to upload to Azure Storage. Azure limits the size of each block to a maximum of 4MB.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoRest
Boolean iBTls
Integer iPort
Boolean iBAutoReconnect
Variant vAzAuth
Handle hoAzAuth
Handle hoXml
Handle hoFac
Integer iBlockSize
Integer iNumBlocks
Variant vSbResponseBody
Handle hoSbResponseBody
Handle hoUriPath
String sBlockId
Variant vDataBlock
Handle hoDataBlock
Integer i
String sXmlStr
String sResponseStr
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// Azure Blob Service Example: Upload a file in blocks, and then commit the block list.
// See also: https://msdn.microsoft.com/en-us/library/azure/dd135726.aspx
// 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
// Note: The application does not need to explicitly set the following
// headers: Content-Length, x-ms-date, Authorization. These headers
// are automatically set by Chilkat.
// As the blocks are uploaded, we'll keep an XML block list for the subsequent commit..
Get Create (RefClass(cComChilkatXml)) To hoXml
If (Not(IsComObjectCreated(hoXml))) Begin
Send CreateComObject of hoXml
End
Set ComTag Of hoXml To "BlockList"
// Any type of file can be uploaded in this way. It can a text file, binary file, anything...
// This example will upload an XML file that is approximately 275K in size. It can be downloaded
// at http://www.chilkatsoft.com/hamlet.xml
Get Create (RefClass(cComCkFileAccess)) To hoFac
If (Not(IsComObjectCreated(hoFac))) Begin
Send CreateComObject of hoFac
End
Get ComOpenForRead Of hoFac "qa_data/xml/hamlet.xml" To iSuccess
// Assuming success for the example..
// We'll upload in 16K blocks (normally a program would upload in larger block sizes than this,
// but this is just an example...)
Move 16384 To iBlockSize
// How many 16K blocks? (Including 1 for the last partial block)
Get ComGetNumBlocks Of hoFac iBlockSize To iNumBlocks
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
Send CreateComObject of hoSbResponseBody
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoUriPath
If (Not(IsComObjectCreated(hoUriPath))) Begin
Send CreateComObject of hoUriPath
End
Get Create (RefClass(cComChilkatBinData)) To hoDataBlock
If (Not(IsComObjectCreated(hoDataBlock))) Begin
Send CreateComObject of hoDataBlock
End
Move 0 To i
While (i < iNumBlocks)
Get ComClear Of hoDataBlock To iSuccess
Get pvComObject of hoDataBlock to vDataBlock
Get ComReadBlockBd Of hoFac i iBlockSize vDataBlock To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoFac To sTemp1
Showln sTemp1
Procedure_Return
End
// Generate a base64 block ID.
// (Chilkat provides a helper method named GenBlockId to make this easy)
// A pre-base64 encoded block ID length of 4 is sufficient in this case because
// this file certainly won't have more than 99,999 blocks..
Get ComGenBlockId Of hoFac i 4 "base64" To sBlockId
// Add this blockId to the list of blocks to be committed.
Send ComNewChild2 To hoXml "Latest" sBlockId
// Build the URI path
Send ComClear To hoUriPath
Get ComAppend Of hoUriPath "/mycontainer/hamlet.xml?comp=block&blockId=" To iSuccess
Get ComAppend Of hoUriPath sBlockId To iSuccess
// Upload this block..
Send ComClear To hoSbResponseBody
Get ComGetAsString Of hoUriPath To sTemp1
Get pvComObject of hoDataBlock to vDataBlock
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComFullRequestBd Of hoRest "PUT" sTemp1 vDataBlock vSbResponseBody To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// Verify that we received a 201 status code.
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 201) Begin
// 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
Get ComGetAsString Of hoSbResponseBody To sTemp1
Showln "response body (if any): " sTemp1
Showln "---"
Get ComLastRequestStartLine Of hoRest To sTemp1
Showln "LastRequestStartLine: " sTemp1
Get ComLastRequestHeader Of hoRest To sTemp1
Showln "LastRequestHeader: " sTemp1
Procedure_Return
End
Move (i + 1) To i
Loop
Send ComFileClose To hoFac
// Now commit the blocks.
// Let's have a look at the XML that will commit the blocks:
Get ComGetXml Of hoXml To sXmlStr
Showln sXmlStr
// The XML will look like this:
// <?xml version="1.0" encoding="utf-8" ?>
// <BlockList>
// <Latest>MDAwMA==</Latest>
// <Latest>MDAwMQ==</Latest>
// <Latest>MDAwMg==</Latest>
// <Latest>MDAwMw==</Latest>
// <Latest>MDAwNA==</Latest>
// <Latest>MDAwNQ==</Latest>
// <Latest>MDAwNg==</Latest>
// <Latest>MDAwNw==</Latest>
// <Latest>MDAwOA==</Latest>
// <Latest>MDAwOQ==</Latest>
// <Latest>MDAxMA==</Latest>
// <Latest>MDAxMQ==</Latest>
// <Latest>MDAxMg==</Latest>
// <Latest>MDAxMw==</Latest>
// <Latest>MDAxNA==</Latest>
// <Latest>MDAxNQ==</Latest>
// <Latest>MDAxNg==</Latest>
// <Latest>MDAxNw==</Latest>
// </BlockList>
// Send the PUT Block List...
Get ComFullRequestString Of hoRest "PUT" "/mycontainer/hamlet.xml?comp=blocklist" sXmlStr To sResponseStr
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// When successful, the Azure Storage service will respond with a 201 response status code,
// with no response body.
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 201) Begin
// 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 "response body (if any): " sResponseStr
Showln "---"
Get ComLastRequestStartLine Of hoRest To sTemp1
Showln "LastRequestStartLine: " sTemp1
Get ComLastRequestHeader Of hoRest To sTemp1
Showln "LastRequestHeader: " sTemp1
Procedure_Return
End
Showln "Success."
End_Procedure