Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oAzAuth
LOCAL oXml
LOCAL oFac
LOCAL nBlockSize
LOCAL nNumBlocks
LOCAL oCrypt
LOCAL oSbResponseBody
LOCAL oUriPath
LOCAL cBlockId
LOCAL oDataBlock
LOCAL cContentMd5
LOCAL i
LOCAL cXmlStr
LOCAL cResponseStr

nSuccess := 0

//  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 := CreateObject("Chilkat.Rest")

//  Connect to the Azure Storage Blob Service
nBTls := 1
nPort := 443
nBAutoReconnect := 1
//  In this example, the storage account name is "chilkat".
nSuccess := oRest:Connect("chilkat.blob.core.windows.net", nPort, nBTls, nBAutoReconnect)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    RETURN
ENDIF

//  Provide Azure Cloud credentials for the REST call.
oAzAuth := CreateObject("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"
nSuccess := 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 := CreateObject("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 := CreateObject("Chilkat.FileAccess")
nSuccess := 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...)
nBlockSize := 16384

//  How many 16K blocks?  (Including 1 for the last partial block)
nNumBlocks := oFac:GetNumBlocks(nBlockSize)

oCrypt := CreateObject("Chilkat.Crypt2")
oCrypt:HashAlgorithm := "md5"

oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oUriPath := CreateObject("Chilkat.StringBuilder")

oDataBlock := CreateObject("Chilkat.BinData")

i := 0
DO WHILE i < nNumBlocks

    oDataBlock:Clear()
    nSuccess := oFac:ReadBlockBd(i, nBlockSize, oDataBlock)
    IF (nSuccess == 0)
        ? oFac:LastErrorText
        oRest:destroy()
        oAzAuth:destroy()
        oXml:destroy()
        oFac:destroy()
        oCrypt:destroy()
        oSbResponseBody:destroy()
        oUriPath:destroy()
        oDataBlock:destroy()
        RETURN
    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..
    cBlockId := oFac:GenBlockId(i, 4, "base64")

    //  Add this blockId to the list of blocks to be committed.
    oXml:NewChild2("Latest", cBlockId)

    //  Build the URI path
    oUriPath:Clear()
    nSuccess := oUriPath:Append("/mycontainer/hamlet.xml?comp=block&blockId=")
    nSuccess := oUriPath:Append(cBlockId)

    cContentMd5 := oCrypt:HashBdENC(oDataBlock)
    oRest:AddHeader("Content-MD5", cContentMd5)

    //  Upload this block..
    oSbResponseBody:Clear()
    nSuccess := oRest:FullRequestBd("PUT", oUriPath:GetAsString(), oDataBlock, oSbResponseBody)
    IF (nSuccess == 0)
        ? oRest:LastErrorText
        oRest:destroy()
        oAzAuth:destroy()
        oXml:destroy()
        oFac:destroy()
        oCrypt:destroy()
        oSbResponseBody:destroy()
        oUriPath:destroy()
        oDataBlock:destroy()
        RETURN
    ENDIF

    //  Verify that we received a 201 status code.
    IF (oRest:ResponseStatusCode != 201)
        //  Examine the request/response to see what happened.
        ? "response status code = " + Str(oRest:ResponseStatusCode)
        ? "response status text = " + oRest:ResponseStatusText
        ? "response header: " + oRest:ResponseHeader
        ? "response body (if any): " + oSbResponseBody:GetAsString()
        ? "---"
        ? "LastRequestStartLine: " + oRest:LastRequestStartLine
        ? "LastRequestHeader: " + oRest:LastRequestHeader
        oRest:destroy()
        oAzAuth:destroy()
        oXml:destroy()
        oFac:destroy()
        oCrypt:destroy()
        oSbResponseBody:destroy()
        oUriPath:destroy()
        oDataBlock:destroy()
        RETURN
    ENDIF

    i := i + 1
ENDDO

oFac:FileClose()

//  Now commit the blocks.
//  Let's have a look at the XML that will commit the blocks:
cXmlStr := oXml:GetXml()
? cXmlStr

//  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...
cResponseStr := oRest:FullRequestString("PUT", "/mycontainer/hamlet.xml?comp=blocklist", cXmlStr)
IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oAzAuth:destroy()
    oXml:destroy()
    oFac:destroy()
    oCrypt:destroy()
    oSbResponseBody:destroy()
    oUriPath:destroy()
    oDataBlock:destroy()
    RETURN
ENDIF

//  When successful, the Azure Storage service will respond with a 201 response status code,
//  with no response body.

IF (oRest:ResponseStatusCode != 201)
    //  Examine the request/response to see what happened.
    ? "response status code = " + Str(oRest:ResponseStatusCode)
    ? "response status text = " + oRest:ResponseStatusText
    ? "response header: " + oRest:ResponseHeader
    ? "response body (if any): " + cResponseStr
    ? "---"
    ? "LastRequestStartLine: " + oRest:LastRequestStartLine
    ? "LastRequestHeader: " + oRest:LastRequestHeader
    oRest:destroy()
    oAzAuth:destroy()
    oXml:destroy()
    oFac:destroy()
    oCrypt:destroy()
    oSbResponseBody:destroy()
    oUriPath:destroy()
    oDataBlock:destroy()
    RETURN
ENDIF

? "Success."

oRest:destroy()
oAzAuth:destroy()
oXml:destroy()
oFac:destroy()
oCrypt:destroy()
oSbResponseBody:destroy()
oUriPath:destroy()
oDataBlock:destroy()