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