Azure Blob: Upload Blocks with Content-MD5 and Commit
See more Azure Cloud Storage Examples
Stage 4 MiB blocks and commit them in order. The current service limits are 4,000 MiB per block and 50,000 committed blocks; this example chooses smaller blocks to bound memory. Every block ID has the same decoded length, includes a per-upload prefix, and is passed as the lowercase blockid query parameter through AddQueryParam for correct escaping.
Only the commit makes the new content visible. If-None-Match on commit prevents replacing an existing blob. A failed staging or commit can leave uncommitted blocks; this sample is not a checkpoint/resume implementation. Prevent concurrent writers and do not change the source while uploading. An empty input commits an empty list.
Content-MD5 is the Base64 MD5 of each block, not hexadecimal. It verifies transport of that block and is removed before committing XML. It does not automatically become the full blob content MD5.
Uses direct Chilkat REST calls with HTTPS certificate verification. Shared Key examples load a private qa_data/azure/storage.json file containing string fields account and access_key (the base64 account key). SAS consumers need only account there. Create local input/output directories before running. Examples target Azure public-cloud endpoints; adapt hostnames for other clouds or custom endpoints.
REST examples pin x-ms-version: 2026-04-06, and SAS generation uses the same service version. Complete-URL downloads use the supplied SAS version. This is a fully deployed version documented by Microsoft at review time. Older supported versions are not automatically obsolete. Transport completion and HTTP success are checked separately; writes are not automatically replayed after a lost response. Keep diagnostic dumps, keys and SAS URLs private.
Shared Key remains supported where allowed by account policy. Microsoft recommends Microsoft Entra ID, preferably managed identities where available, and user delegation SAS for delegated Blob access. Account-key SAS and Shared Key requests can be rejected when Shared Key access is disabled; these samples do not bypass that policy. See Azure data authorization.
Microsoft put block list reference.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loTextCompare
LOCAL loConfig
LOCAL lnSameText1
LOCAL lnSameText2
LOCAL loEndpoint
LOCAL lcResourcePath
LOCAL loRest
LOCAL loAzAuth
LOCAL loConnection
LOCAL lcResponseText
LOCAL loResponseBody
LOCAL loResult
LOCAL loInput
LOCAL lnBlockSize
LOCAL lnNumBlocks
LOCAL loRandom
LOCAL lcUploadId
LOCAL loBlocks
LOCAL loChunk
LOCAL loBlockText
LOCAL i
LOCAL lcBlockId
LOCAL lcDigest
lnSuccess = 0
* StringBuilder compares text by value in every generated language.
loTextCompare = CreateObject('Chilkat.StringBuilder')
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loConfig = CreateObject('Chilkat.JsonObject')
loConfig.Strict = 1
lnSuccess = loConfig.LoadFile("qa_data/azure/storage.json")
IF (lnSuccess <> 1) THEN
? "Could not load private Azure configuration."
RELEASE loTextCompare
RELEASE loConfig
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loConfig.StringOf("account"))
lnSameText1 = loTextCompare.ContentsEqual("",1)
IF ((loConfig.JsonTypeOf("account") <> 1) OR (lnSameText1 = 1)) THEN
? "Supply the storage account name."
RELEASE loTextCompare
RELEASE loConfig
CANCEL
ENDIF
* access_key is the base64 account key, not a connection string or SAS.
loTextCompare.Clear()
loTextCompare.SetString(loConfig.StringOf("access_key"))
lnSameText2 = loTextCompare.ContentsEqual("",1)
IF ((loConfig.JsonTypeOf("access_key") <> 1) OR (lnSameText2 = 1)) THEN
? "Supply the base64 account access key."
RELEASE loTextCompare
RELEASE loConfig
CANCEL
ENDIF
loEndpoint = CreateObject('Chilkat.StringBuilder')
lnSuccess = loEndpoint.Append(loConfig.StringOf("account"))
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loEndpoint.Append(".blob.core.windows.net")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
CANCEL
ENDIF
* resourcePath is already URL-encoded. Encode each path segment once when adapting it.
* Keep slash separators; names and paths are case-sensitive.
lcResourcePath = "/examplecontainer/example.bin"
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
loAzAuth = CreateObject('Chilkat.AuthAzureStorage')
loAzAuth.Account = loConfig.StringOf("account")
loAzAuth.AccessKey = loConfig.StringOf("access_key")
loAzAuth.Service = "Blob"
loAzAuth.Scheme = "SharedKey"
loAzAuth.XMsVersion = "2026-04-06"
lnSuccess = loRest.SetAuthAzureStorage(loAzAuth)
IF (lnSuccess <> 1) THEN
? "Could not configure Shared Key signing."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
CANCEL
ENDIF
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
? "TLS connection failed; inspect diagnostics privately."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
CANCEL
ENDIF
* Do not replay a write automatically after losing its response.
lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
CANCEL
ENDIF
loResponseBody = CreateObject('Chilkat.StringBuilder')
loResult = CreateObject('Chilkat.Xml')
loResult.Strict = 1
loInput = CreateObject('Chilkat.FileAccess')
lnSuccess = loInput.OpenForRead("qa_data/bin/sample.bin")
IF (lnSuccess <> 1) THEN
? "Could not open input."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
CANCEL
ENDIF
lnBlockSize = 4194304
lnNumBlocks = loInput.GetNumBlocks(lnBlockSize)
IF ((lnNumBlocks < 0) OR (lnNumBlocks > 50000)) THEN
? "Input exceeds the demonstration block limit."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
CANCEL
ENDIF
loRandom = CreateObject('Chilkat.Crypt2')
lcUploadId = loRandom.GenerateUuid()
IF (loRandom.LastMethodSuccess <> 1) THEN
? "Could not create upload ID."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
CANCEL
ENDIF
* Persist uploadId, block IDs and the unchanged source fingerprint to implement resumption.
loBlocks = CreateObject('Chilkat.Xml')
loBlocks.Tag = "BlockList"
loChunk = CreateObject('Chilkat.BinData')
loBlockText = CreateObject('Chilkat.StringBuilder')
i = 0
DO WHILE (i < lnNumBlocks)
lnSuccess = loChunk.Clear()
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loInput.ReadBlockBd(i,lnBlockSize,loChunk)
IF (lnSuccess <> 1) THEN
? "Failed to read block."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
loBlockText.Clear()
lnSuccess = loBlockText.Append(lcUploadId)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loBlockText.Append(loInput.GenBlockId(i,5,"ascii"))
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loBlockText.Encode("base64","utf-8")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lcBlockId = loBlockText.GetAsString()
lnSuccess = loRest.ClearAllQueryParams()
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("comp","block")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("blockid",lcBlockId)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lcDigest = loChunk.GetHash("md5","base64")
IF (loChunk.LastMethodSuccess <> 1) THEN
? "Could not hash block."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loRest.AddHeader("Content-MD5",lcDigest)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
loResponseBody.Clear()
lnSuccess = loRest.FullRequestBd("PUT",lcResourcePath,loChunk,loResponseBody)
IF (lnSuccess <> 1) THEN
? "Request did not complete; outcome may be uncertain."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
IF (loRest.ResponseStatusCode <> 201) THEN
? "HTTP status: " + STR(loRest.ResponseStatusCode)
? "Azure error code: " + loRest.ResponseHdrByName("x-ms-error-code")
? "Request ID: " + loRest.ResponseHdrByName("x-ms-request-id")
* Inspect any error body privately; do not log credentials, SAS URLs or payloads.
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
* Add only a successfully staged block, in source order.
loBlocks.NewChild2("Uncommitted",lcBlockId)
i = i + 1
loRest.Disconnect(10)
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
? "TLS reconnection failed."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
loInput.FileClose()
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
ENDDO
loInput.FileClose()
lnSuccess = loRest.ClearAllQueryParams()
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("comp","blocklist")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loRest.RemoveHeader("Content-MD5")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loRest.AddHeader("Content-Type","application/xml")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loRest.AddHeader("x-ms-blob-content-type","application/octet-stream")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lnSuccess = loRest.AddHeader("If-None-Match","*")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
lcResponseText = loRest.FullRequestString("PUT",lcResourcePath,loBlocks.GetXml())
IF (loRest.LastMethodSuccess <> 1) THEN
? "Request did not complete; outcome may be uncertain."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
IF (loRest.ResponseStatusCode <> 201) THEN
? "HTTP status: " + STR(loRest.ResponseStatusCode)
? "Azure error code: " + loRest.ResponseHdrByName("x-ms-error-code")
? "Request ID: " + loRest.ResponseHdrByName("x-ms-request-id")
* Inspect any error body privately; do not log credentials, SAS URLs or payloads.
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText
CANCEL
ENDIF
? "Block list committed; blob created."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResponseBody
RELEASE loResult
RELEASE loInput
RELEASE loRandom
RELEASE loBlocks
RELEASE loChunk
RELEASE loBlockText