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 B4X Downloads
Dim success As Boolean = False
' StringBuilder compares text by value in every generated language.
Dim textCompare As ChilkatStringBuilder
textCompare.Initialize
' Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
Dim config As ChilkatJsonObject
config.Initialize
config.Strict = True
success = config.LoadFile("qa_data/azure/storage.json")
If success <> True Then
Log("Could not load private Azure configuration.")
Return
End If
textCompare.Clear
textCompare.SetString(config.StringOf("account"))
Dim sameText1 As Boolean = textCompare.ContentsEqual("", True)
If (config.JsonTypeOf("account") <> 1) Or (sameText1 = True) Then
Log("Supply the storage account name.")
Return
End If
' access_key is the base64 account key, not a connection string or SAS.
textCompare.Clear
textCompare.SetString(config.StringOf("access_key"))
Dim sameText2 As Boolean = textCompare.ContentsEqual("", True)
If (config.JsonTypeOf("access_key") <> 1) Or (sameText2 = True) Then
Log("Supply the base64 account access key.")
Return
End If
Dim endpoint As ChilkatStringBuilder
endpoint.Initialize
success = endpoint.Append(config.StringOf("account"))
If success <> True Then
Log("Could not prepare the request.")
Return
End If
success = endpoint.Append(".blob.core.windows.net")
If success <> True Then
Log("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 = "/examplecontainer/example.bin"
Dim rest As ChilkatRest
rest.Initialize("rest")
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
Dim azAuth As ChilkatAuthAzureStorage
azAuth.Initialize
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
Log("Could not configure Shared Key signing.")
Return
End If
Dim connection As ChilkatSocket
connection.Initialize("connection")
connection.RequireSslCertVerify = True
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString, 443, True, 20000)
If success <> True Then
Log("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
Log("Could not prepare the request.")
Return
End If
Dim responseText As String
Dim responseBody As ChilkatStringBuilder
responseBody.Initialize
Dim result As ChilkatXml
result.Initialize
result.Strict = True
Dim sourceFile As String = "qa_data/bin/sample.bin"
Dim checkpointFile As String = "qa_data/azure/upload-checkpoint.json"
Dim input As ChilkatFileAccess
input.Initialize
success = input.OpenForRead(sourceFile)
If success <> True Then
Log("Could not open upload source.")
input.FileClose
Return
End If
Dim sourceSize As String = input.FileSizeStr(sourceFile)
If input.LastMethodSuccess <> True Then
Log("Could not read source size.")
input.FileClose
Return
End If
Dim blockSize As Int = 4194304
Dim numBlocks As Int = input.GetNumBlocks(blockSize)
If (input.LastMethodSuccess <> True) Or (numBlocks < 0) Or (numBlocks > 50000) Then
Log("Source exceeds the 50,000-block demonstration bound.")
input.FileClose
Return
End If
Dim hash As ChilkatCrypt2
hash.Initialize("hash")
hash.HashAlgorithm = "sha256"
hash.EncodingMode = "hex_lower"
Dim fingerprint As String = hash.HashFileENC(sourceFile)
If hash.LastMethodSuccess <> True Then
Log("Could not fingerprint source.")
input.FileClose
Return
End If
Dim checkpoint As ChilkatJsonObject
checkpoint.Initialize
checkpoint.Strict = True
Dim files As ChilkatFileAccess
files.Initialize
If files.FileExists(checkpointFile) = True Then
success = checkpoint.LoadFile(checkpointFile)
If success <> True Then
Log("Invalid checkpoint; do not silently start another upload.")
input.FileClose
Return
End If
textCompare.Clear
textCompare.SetString(checkpoint.StringOf("path"))
Dim sameText3 As Boolean = textCompare.ContentsEqual(resourcePath, True)
textCompare.Clear
textCompare.SetString(checkpoint.StringOf("sha256"))
Dim sameText4 As Boolean = textCompare.ContentsEqual(fingerprint, True)
textCompare.Clear
textCompare.SetString(checkpoint.StringOf("size"))
Dim sameText5 As Boolean = 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
Log("Checkpoint target or source changed. Choose a new checkpoint/target deliberately.")
input.FileClose
Return
End If
textCompare.Clear
textCompare.SetString(checkpoint.StringOf("upload_id"))
Dim sameText6 As Boolean = textCompare.ContentsEqual("", True)
If sameText6 = True Then
Log("Missing checkpoint upload ID.")
input.FileClose
Return
End If
Else
Dim newId As String = hash.GenerateUuid
If hash.LastMethodSuccess <> True Then
Log("Could not generate upload ID.")
input.FileClose
Return
End If
success = checkpoint.UpdateString("account", config.StringOf("account"))
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.UpdateString("path", resourcePath)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.UpdateString("sha256", fingerprint)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.UpdateString("size", sourceSize)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.UpdateInt("block_size", blockSize)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.UpdateString("upload_id", newId)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = checkpoint.WriteFile("qa_data/azure/upload-checkpoint.tmp")
If success <> True Then
Log("Could not write checkpoint.")
input.FileClose
Return
End If
success = files.FileRename("qa_data/azure/upload-checkpoint.tmp", checkpointFile)
If success <> True Then
Log("Could not publish checkpoint.")
input.FileClose
Return
End If
End If
Dim uploadId As String = checkpoint.StringOf("upload_id")
Dim idCheck As ChilkatStringBuilder
idCheck.Initialize
success = idCheck.Append(uploadId)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
If idCheck.Length <> 36 Then
Log("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
Log("Could not inspect destination.")
input.FileClose
Return
End If
If rest.ResponseStatusCode = 200 Then
textCompare.Clear
textCompare.SetString(rest.ResponseHdrByName("x-ms-meta-upload-id"))
Dim sameText7 As Boolean = textCompare.ContentsEqual(uploadId, True)
textCompare.Clear
textCompare.SetString(rest.ResponseHdrByName("x-ms-meta-source-sha256"))
Dim sameText8 As Boolean = textCompare.ContentsEqual(fingerprint, True)
textCompare.Clear
textCompare.SetString(rest.ResponseHdrByName("Content-Length"))
Dim sameText9 As Boolean = textCompare.ContentsEqual(sourceSize, True)
If (sameText7 <> True) Or (sameText8 <> True) Or (sameText9 <> True) Then
Log("Destination exists and is not this completed upload. Refusing to replace it.")
input.FileClose
Return
End If
Log("Upload already committed. Keep the checkpoint until the application records completion.")
input.FileClose
Return
End If
textCompare.Clear
textCompare.SetString(rest.ResponseHdrByName("x-ms-error-code"))
Dim sameText10 As Boolean = textCompare.ContentsEqual("BlobNotFound", True)
If (rest.ResponseStatusCode <> 404) Or (sameText10 <> True) Then
Log("Expected an absent blob in an existing container.")
input.FileClose
Return
End If
success = rest.AddQueryParam("comp", "blocklist")
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = rest.AddQueryParam("blocklisttype", "uncommitted")
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
rest.Disconnect(10)
success = connection.Connect(endpoint.GetAsString, 443, True, 20000)
If success <> True Then
Log("TLS reconnection failed.")
input.FileClose
Return
End If
success = rest.UseConnection(connection, False)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
responseText = rest.FullRequestNoBody("GET", resourcePath)
If rest.LastMethodSuccess <> True Then
Log("Could not reconcile uploaded blocks.")
input.FileClose
Return
End If
Dim remoteBlocks As ChilkatHashtable
remoteBlocks.Initialize
If rest.ResponseStatusCode = 200 Then
success = result.LoadXml(responseText)
If success <> True Then
Log("Expected well-formed XML.")
input.FileClose
Return
End If
If result.TagEquals("BlockList") <> True Then
Log("Unexpected XML response root.")
input.FileClose
Return
End If
Dim remoteCount As Int = result.NumChildrenAt("UncommittedBlocks")
Dim j As Int = 0
Do While (j < remoteCount)
result.I = j
Dim remoteId As String = result.GetChildContent("UncommittedBlocks|Block[i]|Name")
Dim remoteSize As String = result.GetChildContent("UncommittedBlocks|Block[i]|Size")
textCompare.Clear
textCompare.SetString(remoteId)
Dim sameText11 As Boolean = textCompare.ContentsEqual("", True)
textCompare.Clear
textCompare.SetString(remoteSize)
Dim sameText12 As Boolean = textCompare.ContentsEqual("", True)
If (sameText11 = True) Or (sameText12 = True) Then
Log("Malformed remote block listing.")
input.FileClose
Return
End If
success = remoteBlocks.AddStr(remoteId, remoteSize)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
j = j + 1
Loop
Else
textCompare.Clear
textCompare.SetString(rest.ResponseHdrByName("x-ms-error-code"))
Dim sameText13 As Boolean = textCompare.ContentsEqual("BlobNotFound", True)
If (rest.ResponseStatusCode <> 404) Or (sameText13 <> True) Then
Log("Block listing failed.")
input.FileClose
Return
End If
End If
Dim blocks As ChilkatXml
blocks.Initialize
blocks.Tag = "BlockList"
Dim chunk As ChilkatBinData
chunk.Initialize
Dim blockText As ChilkatStringBuilder
blockText.Initialize
Dim sizeText As ChilkatStringBuilder
sizeText.Initialize
Dim i As Int = 0
Do While (i < numBlocks)
success = chunk.Clear
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = input.ReadBlockBd(i, blockSize, chunk)
If success <> True Then
Log("Could not read source block.")
input.FileClose
Return
End If
If (chunk.NumBytes < 1) Or ((i < numBlocks - 1) And (chunk.NumBytes <> blockSize)) Then
Log("Source changed or an incomplete read occurred.")
input.FileClose
Return
End If
blockText.Clear
success = blockText.Append(uploadId)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = blockText.Append(input.GenBlockId(i, 5, "ascii"))
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = blockText.Encode("base64", "utf-8")
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
Dim blockId As String = blockText.GetAsString
sizeText.Clear
success = sizeText.AppendInt(chunk.NumBytes)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
Dim uploadBlock As Boolean = True
If remoteBlocks.Contains(blockId) = True Then
If remoteBlocks.LookupStr(blockId) <> sizeText.GetAsString Then
Log("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
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = rest.AddQueryParam("comp", "block")
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
success = rest.AddQueryParam("blockid", blockId)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
rest.Disconnect(10)
success = connection.Connect(endpoint.GetAsString, 443, True, 20000)
If success <> True Then
Log("TLS reconnection failed.")
input.FileClose
Return
End If
success = rest.UseConnection(connection, False)
If success <> True Then
Log("Could not prepare the request.")
input.FileClose
Return
End If
responseBody.Clear
success = rest.FullRequestBd("PUT", resourcePath, chunk, responseBody)
If success <> True Then
Log("Request did not complete; outcome may be uncertain.")
input.FileClose
Return
End If
If rest.ResponseStatusCode <> 201 Then
Log("HTTP status: " & rest.ResponseStatusCode)
Log("Azure error code: " & rest.ResponseHdrByName("x-ms-error-code"))
Log("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
Loop
' Rehash before commit. Do not modify the source or run concurrent writers.
Dim finalFingerprint As String = hash.HashFileENC(sourceFile)
textCompare.Clear
textCompare.SetString(finalFingerprint)
Dim sameText14 As Boolean = textCompare.ContentsEqual(fingerprint, True)
If (hash.LastMethodSuccess <> True) Or (sameText14 <> True) Then
Log("Source changed during staging; commit cancelled.")
input.FileClose
Return
End If
input.FileClose
success = rest.ClearAllQueryParams
If success <> True Then
Log("Could not prepare the request.")
Return
End If
success = rest.AddQueryParam("comp", "blocklist")
If success <> True Then
Log("Could not prepare the request.")
Return
End If
success = rest.AddHeader("Content-Type", "application/xml")
If success <> True Then
Log("Could not prepare the request.")
Return
End If
success = rest.AddHeader("x-ms-blob-content-type", "application/octet-stream")
If success <> True Then
Log("Could not prepare the request.")
Return
End If
success = rest.AddHeader("If-None-Match", "*")
If success <> True Then
Log("Could not prepare the request.")
Return
End If
success = rest.AddHeader("x-ms-meta-upload-id", uploadId)
If success <> True Then
Log("Could not prepare the request.")
Return
End If
success = rest.AddHeader("x-ms-meta-source-sha256", fingerprint)
If success <> True Then
Log("Could not prepare the request.")
Return
End If
rest.Disconnect(10)
success = connection.Connect(endpoint.GetAsString, 443, True, 20000)
If success <> True Then
Log("TLS reconnection failed.")
Return
End If
success = rest.UseConnection(connection, False)
If success <> True Then
Log("Could not prepare the request.")
Return
End If
responseText = rest.FullRequestString("PUT", resourcePath, blocks.GetXml)
If rest.LastMethodSuccess <> True Then
Log("Request did not complete; outcome may be uncertain.")
Return
End If
If rest.ResponseStatusCode <> 201 Then
Log("HTTP status: " & rest.ResponseStatusCode)
Log("Azure error code: " & rest.ResponseHdrByName("x-ms-error-code"))
Log("Request ID: " & rest.ResponseHdrByName("x-ms-request-id"))
' Inspect any error body privately; do not log credentials, SAS URLs or payloads.
Return
End If
Log("Upload committed. Retain the checkpoint for reconciliation until completion is recorded.")