AutoIt
AutoIt
Upload File in Blocks (with Content-MD5 header) 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. This example includes a Content-MD5 header for each block.Chilkat AutoIt Downloads
Local $bSuccess = False
; 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.
$oRest = ObjCreate("Chilkat.Rest")
; Connect to the Azure Storage Blob Service
Local $bTls = True
Local $iPort = 443
Local $bAutoReconnect = True
; In this example, the storage account name is "chilkat".
$bSuccess = $oRest.Connect("chilkat.blob.core.windows.net",$iPort,$bTls,$bAutoReconnect)
If ($bSuccess <> True) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
; Provide Azure Cloud credentials for the REST call.
$oAzAuth = ObjCreate("Chilkat.AuthAzureStorage")
$oAzAuth.AccessKey = "AZURE_ACCESS_KEY"
; The account name used here should match the 1st part of the domain passed in the call to Connect (above).
$oAzAuth.Account = "chilkat"
$oAzAuth.Scheme = "SharedKey"
$oAzAuth.Service = "Blob"
; This causes the "x-ms-version: 2021-08-06" header to be automatically added.
$oAzAuth.XMsVersion = "2021-08-06"
$bSuccess = $oRest.SetAuthAzureStorage($oAzAuth)
; 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..
$oXml = ObjCreate("Chilkat.Xml")
$oXml.Tag = "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
$oFac = ObjCreate("Chilkat.FileAccess")
$bSuccess = $oFac.OpenForRead("qa_data/xml/hamlet.xml")
; 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...)
Local $iBlockSize = 16384
; How many 16K blocks? (Including 1 for the last partial block)
Local $iNumBlocks = $oFac.GetNumBlocks($iBlockSize)
$oCrypt = ObjCreate("Chilkat.Crypt2")
$oCrypt.HashAlgorithm = "md5"
$oSbResponseBody = ObjCreate("Chilkat.StringBuilder")
$oUriPath = ObjCreate("Chilkat.StringBuilder")
Local $sBlockId
$oDataBlock = ObjCreate("Chilkat.BinData")
Local $sContentMd5
Local $i = 0
While $i < $iNumBlocks
$oDataBlock.Clear()
$bSuccess = $oFac.ReadBlockBd($i,$iBlockSize,$oDataBlock)
If ($bSuccess = False) Then
ConsoleWrite($oFac.LastErrorText & @CRLF)
Exit
EndIf
; 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..
$sBlockId = $oFac.GenBlockId($i,4,"base64")
; Add this blockId to the list of blocks to be committed.
$oXml.NewChild2 "Latest",$sBlockId
; Build the URI path
$oUriPath.Clear
$bSuccess = $oUriPath.Append("/mycontainer/hamlet.xml?comp=block&blockId=")
$bSuccess = $oUriPath.Append($sBlockId)
$sContentMd5 = $oCrypt.HashBdENC($oDataBlock)
$oRest.AddHeader("Content-MD5",$sContentMd5)
; Upload this block..
$oSbResponseBody.Clear
$bSuccess = $oRest.FullRequestBd("PUT",$oUriPath.GetAsString(),$oDataBlock,$oSbResponseBody)
If ($bSuccess = False) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
; Verify that we received a 201 status code.
If ($oRest.ResponseStatusCode <> 201) Then
; Examine the request/response to see what happened.
ConsoleWrite("response status code = " & $oRest.ResponseStatusCode & @CRLF)
ConsoleWrite("response status text = " & $oRest.ResponseStatusText & @CRLF)
ConsoleWrite("response header: " & $oRest.ResponseHeader & @CRLF)
ConsoleWrite("response body (if any): " & $oSbResponseBody.GetAsString() & @CRLF)
ConsoleWrite("---" & @CRLF)
ConsoleWrite("LastRequestStartLine: " & $oRest.LastRequestStartLine & @CRLF)
ConsoleWrite("LastRequestHeader: " & $oRest.LastRequestHeader & @CRLF)
Exit
EndIf
$i = $i + 1
Wend
$oFac.FileClose
; Now commit the blocks.
; Let's have a look at the XML that will commit the blocks:
Local $sXmlStr = $oXml.GetXml()
ConsoleWrite($sXmlStr & @CRLF)
; 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>
; --------------------------------------------------------------------------
; IMPORTANT: Remove the Content-MD5 header previously set in the loop above.
; --------------------------------------------------------------------------
$oRest.RemoveHeader("Content-MD5")
; Send the PUT Block List...
Local $sResponseStr = $oRest.FullRequestString("PUT","/mycontainer/hamlet.xml?comp=blocklist",$sXmlStr)
If ($oRest.LastMethodSuccess <> True) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
; When successful, the Azure Storage service will respond with a 201 response status code,
; with no response body.
If ($oRest.ResponseStatusCode <> 201) Then
; Examine the request/response to see what happened.
ConsoleWrite("response status code = " & $oRest.ResponseStatusCode & @CRLF)
ConsoleWrite("response status text = " & $oRest.ResponseStatusText & @CRLF)
ConsoleWrite("response header: " & $oRest.ResponseHeader & @CRLF)
ConsoleWrite("response body (if any): " & $sResponseStr & @CRLF)
ConsoleWrite("---" & @CRLF)
ConsoleWrite("LastRequestStartLine: " & $oRest.LastRequestStartLine & @CRLF)
ConsoleWrite("LastRequestHeader: " & $oRest.LastRequestHeader & @CRLF)
Exit
EndIf
ConsoleWrite("Success." & @CRLF)