Azure Blob: Resume a Block Upload from a Checkpoint
See more Azure Cloud Storage Examples
Persist the destination, source size/SHA-256, block size and unique upload ID before staging any blocks. On a later run, verify the source, get the uncommitted block list, skip matching blocks, upload missing blocks, and commit in source order. Block IDs are derived from the saved upload ID and fixed-width index, so a restart uses the same IDs.
Use one process per checkpoint and keep its source and destination exclusively controlled. The example does not replace an existing blob. Commit metadata lets a restart recognize its own completed upload after an uncertain response; otherwise an existing destination is an error. Remote block size is checked, but the source fingerprint and unique namespace are what associate the blocks with this upload. Stale uncommitted blocks can expire; missing blocks are uploaded again. Empty files commit an empty block list.
The sample uses 4 MiB blocks and at most 50,000 blocks; source sizes stay strings and block indexes use int. It fingerprints the whole source at start and before commit, trading read time for change detection. Keep checkpoint files private and unchanged. A corrupt checkpoint fails closed; choose a fresh checkpoint and destination deliberately rather than discarding state automatically.
Uses direct Chilkat.Rest requests and Azure public-cloud endpoints with verified HTTPS. REST requests select service version 2026-04-06. Create local input/output directories before running. Account-key examples read account and access_key string fields from private qa_data/azure/storage.json; do not embed secrets in distributed applications. Entra examples explain their own configuration. Keep credentials, bearer tokens, SAS URLs and diagnostic dumps private. Shared Key is supported only where allowed by account policy; Entra authentication is preferred where available.
Transport success and HTTP status are checked separately. Writes are not automatically replayed after an uncertain response. IDs, ETags, continuation markers and file sizes are strings; counters and explicitly bounded offsets use int. No webhook or web server is required.
Chilkat Xojo Plugin Downloads
Dim success As Boolean
success = False
// StringBuilder compares text by value in every generated language.
Dim textCompare As New Chilkat.StringBuilder
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
Dim config As New Chilkat.JsonObject
config.Strict = True
success = config.LoadFile("qa_data/azure/storage.json")
If (success <> True) Then
System.DebugLog("Could not load private Azure configuration.")
Return
End If
textCompare.Clear
success = textCompare.SetString(config.StringOf("account"))
Dim sameText1 As Boolean
sameText1 = textCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("account") <> 1) Or (sameText1 = True)) Then
System.DebugLog("Supply the storage account name.")
Return
End If
// access_key is the base64 account key, not a connection string or SAS.
textCompare.Clear
success = textCompare.SetString(config.StringOf("access_key"))
Dim sameText2 As Boolean
sameText2 = textCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("access_key") <> 1) Or (sameText2 = True)) Then
System.DebugLog("Supply the base64 account access key.")
Return
End If
Dim endpoint As New Chilkat.StringBuilder
success = endpoint.Append(config.StringOf("account"))
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
Return
End If
success = endpoint.Append(".blob.core.windows.net")
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
Return
End If
// resourcePath is already URL-encoded. Encode each path segment once when adapting it.
// Keep slash separators; names and paths are case-sensitive.
Dim resourcePath As String
resourcePath = "/examplecontainer/example.bin"
Dim rest As New Chilkat.Rest
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
Dim azAuth As New Chilkat.AuthAzureStorage
azAuth.Account = config.StringOf("account")
azAuth.AccessKey = config.StringOf("access_key")
azAuth.Service = "Blob"
azAuth.Scheme = "SharedKey"
azAuth.XMsVersion = "2026-04-06"
success = rest.SetAuthAzureStorage(azAuth)
If (success <> True) Then
System.DebugLog("Could not configure Shared Key signing.")
Return
End If
Dim connection As New Chilkat.Socket
connection.RequireSslCertVerify = True
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,True,20000)
If (success <> True) Then
System.DebugLog("TLS connection failed; inspect diagnostics privately.")
Return
End If
// Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,False)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
Return
End If
Dim responseText As String
Dim responseBody As New Chilkat.StringBuilder
Dim result As New Chilkat.Xml
result.Strict = True
Dim sourceFile As String
sourceFile = "qa_data/bin/sample.bin"
Dim checkpointFile As String
checkpointFile = "qa_data/azure/upload-checkpoint.json"
Dim input As New Chilkat.FileAccess
success = input.OpenForRead(sourceFile)
If (success <> True) Then
System.DebugLog("Could not open upload source.")
input.FileClose
Return
End If
Dim sourceSize As String
sourceSize = input.FileSizeStr(sourceFile)
If (input.LastMethodSuccess <> True) Then
System.DebugLog("Could not read source size.")
input.FileClose
Return
End If
Dim blockSize As Int32
blockSize = 4194304
Dim numBlocks As Int32
numBlocks = input.GetNumBlocks(blockSize)
If ((input.LastMethodSuccess <> True) Or (numBlocks < 0) Or (numBlocks > 50000)) Then
System.DebugLog("Source exceeds the 50,000-block demonstration bound.")
input.FileClose
Return
End If
Dim hash As New Chilkat.Crypt2
hash.HashAlgorithm = "sha256"
hash.EncodingMode = "hex_lower"
Dim fingerprint As String
fingerprint = hash.HashFileENC(sourceFile)
If (hash.LastMethodSuccess <> True) Then
System.DebugLog("Could not fingerprint source.")
input.FileClose
Return
End If
Dim checkpoint As New Chilkat.JsonObject
checkpoint.Strict = True
Dim files As New Chilkat.FileAccess
If (files.FileExists(checkpointFile) = True) Then
success = checkpoint.LoadFile(checkpointFile)
If (success <> True) Then
System.DebugLog("Invalid checkpoint; do not silently start another upload.")
input.FileClose
Return
End If
textCompare.Clear
success = textCompare.SetString(checkpoint.StringOf("path"))
Dim sameText3 As Boolean
sameText3 = textCompare.ContentsEqual(resourcePath,True)
textCompare.Clear
success = textCompare.SetString(checkpoint.StringOf("sha256"))
Dim sameText4 As Boolean
sameText4 = textCompare.ContentsEqual(fingerprint,True)
textCompare.Clear
success = textCompare.SetString(checkpoint.StringOf("size"))
Dim sameText5 As Boolean
sameText5 = textCompare.ContentsEqual(sourceSize,True)
If ((checkpoint.StringOf("account") <> config.StringOf("account")) Or (sameText3 <> True) Or (sameText4 <> True) Or (sameText5 <> True) Or (checkpoint.IntOf("block_size") <> blockSize)) Then
System.DebugLog("Checkpoint target or source changed. Choose a new checkpoint/target deliberately.")
input.FileClose
Return
End If
textCompare.Clear
success = textCompare.SetString(checkpoint.StringOf("upload_id"))
Dim sameText6 As Boolean
sameText6 = textCompare.ContentsEqual("",True)
If (sameText6 = True) Then
System.DebugLog("Missing checkpoint upload ID.")
input.FileClose
Return
End If
Else
Dim newId As String
newId = hash.GenerateUuid()
If (hash.LastMethodSuccess <> True) Then
System.DebugLog("Could not generate upload ID.")
input.FileClose
Return
End If
success = checkpoint.UpdateString("account",config.StringOf("account"))
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.UpdateString("path",resourcePath)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.UpdateString("sha256",fingerprint)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.UpdateString("size",sourceSize)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.UpdateInt("block_size",blockSize)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.UpdateString("upload_id",newId)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.WriteFile("qa_data/azure/upload-checkpoint.tmp")
If (success <> True) Then
System.DebugLog("Could not write checkpoint.")
input.FileClose
Return
End If
success = files.FileRename("qa_data/azure/upload-checkpoint.tmp",checkpointFile)
If (success <> True) Then
System.DebugLog("Could not publish checkpoint.")
input.FileClose
Return
End If
End If
Dim uploadId As String
uploadId = checkpoint.StringOf("upload_id")
Dim idCheck As New Chilkat.StringBuilder
success = idCheck.Append(uploadId)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
If (idCheck.Length <> 36) Then
System.DebugLog("Invalid upload ID length in checkpoint.")
input.FileClose
Return
End If
// Reconcile an uncertain commit before staging anything else.
responseText = rest.FullRequestNoBody("HEAD",resourcePath)
If (rest.LastMethodSuccess <> True) Then
System.DebugLog("Could not inspect destination.")
input.FileClose
Return
End If
If (rest.ResponseStatusCode = 200) Then
textCompare.Clear
success = textCompare.SetString(rest.ResponseHdrByName("x-ms-meta-upload-id"))
Dim sameText7 As Boolean
sameText7 = textCompare.ContentsEqual(uploadId,True)
textCompare.Clear
success = textCompare.SetString(rest.ResponseHdrByName("x-ms-meta-source-sha256"))
Dim sameText8 As Boolean
sameText8 = textCompare.ContentsEqual(fingerprint,True)
textCompare.Clear
success = textCompare.SetString(rest.ResponseHdrByName("Content-Length"))
Dim sameText9 As Boolean
sameText9 = textCompare.ContentsEqual(sourceSize,True)
If ((sameText7 <> True) Or (sameText8 <> True) Or (sameText9 <> True)) Then
System.DebugLog("Destination exists and is not this completed upload. Refusing to replace it.")
input.FileClose
Return
End If
System.DebugLog("Upload already committed. Keep the checkpoint until the application records completion.")
input.FileClose
Return
End If
textCompare.Clear
success = textCompare.SetString(rest.ResponseHdrByName("x-ms-error-code"))
Dim sameText10 As Boolean
sameText10 = textCompare.ContentsEqual("BlobNotFound",True)
If ((rest.ResponseStatusCode <> 404) Or (sameText10 <> True)) Then
System.DebugLog("Expected an absent blob in an existing container.")
input.FileClose
Return
End If
success = rest.AddQueryParam("comp","blocklist")
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = rest.AddQueryParam("blocklisttype","uncommitted")
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = rest.Disconnect(10)
success = connection.Connect(endpoint.GetAsString(),443,True,20000)
If (success <> True) Then
System.DebugLog("TLS reconnection failed.")
input.FileClose
Return
End If
success = rest.UseConnection(connection,False)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
responseText = rest.FullRequestNoBody("GET",resourcePath)
If (rest.LastMethodSuccess <> True) Then
System.DebugLog("Could not reconcile uploaded blocks.")
input.FileClose
Return
End If
Dim remoteBlocks As New Chilkat.Hashtable
If (rest.ResponseStatusCode = 200) Then
success = result.LoadXml(responseText)
If (success <> True) Then
System.DebugLog("Expected well-formed XML.")
input.FileClose
Return
End If
If (result.TagEquals("BlockList") <> True) Then
System.DebugLog("Unexpected XML response root.")
input.FileClose
Return
End If
Dim remoteCount As Int32
remoteCount = result.NumChildrenAt("UncommittedBlocks")
Dim j As Int32
j = 0
While (j < remoteCount)
result.I = j
Dim remoteId As String
remoteId = result.GetChildContent("UncommittedBlocks|Block[i]|Name")
Dim remoteSize As String
remoteSize = result.GetChildContent("UncommittedBlocks|Block[i]|Size")
textCompare.Clear
success = textCompare.SetString(remoteId)
Dim sameText11 As Boolean
sameText11 = textCompare.ContentsEqual("",True)
textCompare.Clear
success = textCompare.SetString(remoteSize)
Dim sameText12 As Boolean
sameText12 = textCompare.ContentsEqual("",True)
If ((sameText11 = True) Or (sameText12 = True)) Then
System.DebugLog("Malformed remote block listing.")
input.FileClose
Return
End If
success = remoteBlocks.AddStr(remoteId,remoteSize)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
j = j + 1
Wend
Else
textCompare.Clear
success = textCompare.SetString(rest.ResponseHdrByName("x-ms-error-code"))
Dim sameText13 As Boolean
sameText13 = textCompare.ContentsEqual("BlobNotFound",True)
If ((rest.ResponseStatusCode <> 404) Or (sameText13 <> True)) Then
System.DebugLog("Block listing failed.")
input.FileClose
Return
End If
End If
Dim blocks As New Chilkat.Xml
blocks.Tag = "BlockList"
Dim chunk As New Chilkat.BinData
Dim blockText As New Chilkat.StringBuilder
Dim sizeText As New Chilkat.StringBuilder
Dim i As Int32
i = 0
While (i < numBlocks)
success = chunk.Clear()
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = input.ReadBlockBd(i,blockSize,chunk)
If (success <> True) Then
System.DebugLog("Could not read source block.")
input.FileClose
Return
End If
If ((chunk.NumBytes < 1) Or ((i < numBlocks - 1) And (chunk.NumBytes <> blockSize))) Then
System.DebugLog("Source changed or an incomplete read occurred.")
input.FileClose
Return
End If
blockText.Clear
success = blockText.Append(uploadId)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = blockText.Append(input.GenBlockId(i,5,"ascii"))
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = blockText.Encode("base64","utf-8")
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
Dim blockId As String
blockId = blockText.GetAsString()
sizeText.Clear
success = sizeText.AppendInt(chunk.NumBytes)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
Dim uploadBlock As Boolean
uploadBlock = True
If (remoteBlocks.Contains(blockId) = True) Then
If (remoteBlocks.LookupStr(blockId) <> sizeText.GetAsString()) Then
System.DebugLog("Remote block size differs; do not commit this upload.")
input.FileClose
Return
End If
uploadBlock = False
End If
If (uploadBlock = True) Then
success = rest.ClearAllQueryParams()
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = rest.AddQueryParam("comp","block")
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = rest.AddQueryParam("blockid",blockId)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
success = rest.Disconnect(10)
success = connection.Connect(endpoint.GetAsString(),443,True,20000)
If (success <> True) Then
System.DebugLog("TLS reconnection failed.")
input.FileClose
Return
End If
success = rest.UseConnection(connection,False)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
input.FileClose
Return
End If
responseBody.Clear
success = rest.FullRequestBd("PUT",resourcePath,chunk,responseBody)
If (success <> True) Then
System.DebugLog("Request did not complete; outcome may be uncertain.")
input.FileClose
Return
End If
If (rest.ResponseStatusCode <> 201) Then
System.DebugLog("HTTP status: " + Str(rest.ResponseStatusCode))
System.DebugLog("Azure error code: " + rest.ResponseHdrByName("x-ms-error-code"))
System.DebugLog("Request ID: " + rest.ResponseHdrByName("x-ms-request-id"))
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
input.FileClose
Return
End If
End If
blocks.NewChild2 "Uncommitted",blockId
i = i + 1
Wend
// Rehash before commit. Do not modify the source or run concurrent writers.
Dim finalFingerprint As String
finalFingerprint = hash.HashFileENC(sourceFile)
textCompare.Clear
success = textCompare.SetString(finalFingerprint)
Dim sameText14 As Boolean
sameText14 = textCompare.ContentsEqual(fingerprint,True)
If ((hash.LastMethodSuccess <> True) Or (sameText14 <> True)) Then
System.DebugLog("Source changed during staging; commit cancelled.")
input.FileClose
Return
End If
input.FileClose
success = rest.ClearAllQueryParams()
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
Return
End If
success = rest.AddQueryParam("comp","blocklist")
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
Return
End If
success = rest.AddHeader("Content-Type","application/xml")
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
Return
End If
success = rest.AddHeader("x-ms-blob-content-type","application/octet-stream")
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
Return
End If
success = rest.AddHeader("If-None-Match","*")
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
Return
End If
success = rest.AddHeader("x-ms-meta-upload-id",uploadId)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
Return
End If
success = rest.AddHeader("x-ms-meta-source-sha256",fingerprint)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
Return
End If
success = rest.Disconnect(10)
success = connection.Connect(endpoint.GetAsString(),443,True,20000)
If (success <> True) Then
System.DebugLog("TLS reconnection failed.")
Return
End If
success = rest.UseConnection(connection,False)
If (success <> True) Then
System.DebugLog("Could not prepare the request.")
Return
End If
responseText = rest.FullRequestString("PUT",resourcePath,blocks.GetXml())
If (rest.LastMethodSuccess <> True) Then
System.DebugLog("Request did not complete; outcome may be uncertain.")
Return
End If
If (rest.ResponseStatusCode <> 201) Then
System.DebugLog("HTTP status: " + Str(rest.ResponseStatusCode))
System.DebugLog("Azure error code: " + rest.ResponseHdrByName("x-ms-error-code"))
System.DebugLog("Request ID: " + rest.ResponseHdrByName("x-ms-request-id"))
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
Return
End If
System.DebugLog("Upload committed. Retain the checkpoint for reconciliation until completion is recorded.")