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 Ruby Downloads
require 'chilkat'
success = false
# StringBuilder compares text by value in every generated language.
textCompare = Chilkat::CkStringBuilder.new()
# Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
config = Chilkat::CkJsonObject.new()
config.put_Strict(true)
success = config.LoadFile("qa_data/azure/storage.json")
if (success != true)
print "Could not load private Azure configuration." + "\n";
exit
end
textCompare.Clear()
textCompare.SetString(config.stringOf("account"))
sameText1 = textCompare.ContentsEqual("",true)
if ((config.JsonTypeOf("account") != 1) or (sameText1 == true))
print "Supply the storage account name." + "\n";
exit
end
# access_key is the base64 account key, not a connection string or SAS.
textCompare.Clear()
textCompare.SetString(config.stringOf("access_key"))
sameText2 = textCompare.ContentsEqual("",true)
if ((config.JsonTypeOf("access_key") != 1) or (sameText2 == true))
print "Supply the base64 account access key." + "\n";
exit
end
endpoint = Chilkat::CkStringBuilder.new()
success = endpoint.Append(config.stringOf("account"))
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = endpoint.Append(".blob.core.windows.net")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
# 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"
rest = Chilkat::CkRest.new()
rest.put_ConnectTimeoutMs(20000)
rest.put_IdleTimeoutMs(60000)
azAuth = Chilkat::CkAuthAzureStorage.new()
azAuth.put_Account(config.stringOf("account"))
azAuth.put_AccessKey(config.stringOf("access_key"))
azAuth.put_Service("Blob")
azAuth.put_Scheme("SharedKey")
azAuth.put_XMsVersion("2026-04-06")
success = rest.SetAuthAzureStorage(azAuth)
if (success != true)
print "Could not configure Shared Key signing." + "\n";
exit
end
connection = Chilkat::CkSocket.new()
connection.put_RequireSslCertVerify(true)
connection.put_MaxReadIdleMs(60000)
connection.put_MaxSendIdleMs(60000)
success = connection.Connect(endpoint.getAsString(),443,true,20000)
if (success != true)
print "TLS connection failed; inspect diagnostics privately." + "\n";
exit
end
# Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,false)
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
responseBody = Chilkat::CkStringBuilder.new()
result = Chilkat::CkXml.new()
result.put_Strict(true)
sourceFile = "qa_data/bin/sample.bin"
checkpointFile = "qa_data/azure/upload-checkpoint.json"
input = Chilkat::CkFileAccess.new()
success = input.OpenForRead(sourceFile)
if (success != true)
print "Could not open upload source." + "\n";
input.FileClose()
exit
end
sourceSize = input.fileSizeStr(sourceFile)
if (input.get_LastMethodSuccess() != true)
print "Could not read source size." + "\n";
input.FileClose()
exit
end
blockSize = 4194304
numBlocks = input.GetNumBlocks(blockSize)
if ((input.get_LastMethodSuccess() != true) or (numBlocks < 0) or (numBlocks > 50000))
print "Source exceeds the 50,000-block demonstration bound." + "\n";
input.FileClose()
exit
end
hash = Chilkat::CkCrypt2.new()
hash.put_HashAlgorithm("sha256")
hash.put_EncodingMode("hex_lower")
fingerprint = hash.hashFileENC(sourceFile)
if (hash.get_LastMethodSuccess() != true)
print "Could not fingerprint source." + "\n";
input.FileClose()
exit
end
checkpoint = Chilkat::CkJsonObject.new()
checkpoint.put_Strict(true)
files = Chilkat::CkFileAccess.new()
if (files.FileExists(checkpointFile) == true)
success = checkpoint.LoadFile(checkpointFile)
if (success != true)
print "Invalid checkpoint; do not silently start another upload." + "\n";
input.FileClose()
exit
end
textCompare.Clear()
textCompare.SetString(checkpoint.stringOf("path"))
sameText3 = textCompare.ContentsEqual(resourcePath,true)
textCompare.Clear()
textCompare.SetString(checkpoint.stringOf("sha256"))
sameText4 = textCompare.ContentsEqual(fingerprint,true)
textCompare.Clear()
textCompare.SetString(checkpoint.stringOf("size"))
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))
print "Checkpoint target or source changed. Choose a new checkpoint/target deliberately." + "\n";
input.FileClose()
exit
end
textCompare.Clear()
textCompare.SetString(checkpoint.stringOf("upload_id"))
sameText6 = textCompare.ContentsEqual("",true)
if (sameText6 == true)
print "Missing checkpoint upload ID." + "\n";
input.FileClose()
exit
end
else
newId = hash.generateUuid()
if (hash.get_LastMethodSuccess() != true)
print "Could not generate upload ID." + "\n";
input.FileClose()
exit
end
success = checkpoint.UpdateString("account",config.stringOf("account"))
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = checkpoint.UpdateString("path",resourcePath)
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = checkpoint.UpdateString("sha256",fingerprint)
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = checkpoint.UpdateString("size",sourceSize)
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = checkpoint.UpdateInt("block_size",blockSize)
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = checkpoint.UpdateString("upload_id",newId)
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = checkpoint.WriteFile("qa_data/azure/upload-checkpoint.tmp")
if (success != true)
print "Could not write checkpoint." + "\n";
input.FileClose()
exit
end
success = files.FileRename("qa_data/azure/upload-checkpoint.tmp",checkpointFile)
if (success != true)
print "Could not publish checkpoint." + "\n";
input.FileClose()
exit
end
end
uploadId = checkpoint.stringOf("upload_id")
idCheck = Chilkat::CkStringBuilder.new()
success = idCheck.Append(uploadId)
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
if (idCheck.get_Length() != 36)
print "Invalid upload ID length in checkpoint." + "\n";
input.FileClose()
exit
end
# Reconcile an uncertain commit before staging anything else.
responseText = rest.fullRequestNoBody("HEAD",resourcePath)
if (rest.get_LastMethodSuccess() != true)
print "Could not inspect destination." + "\n";
input.FileClose()
exit
end
if (rest.get_ResponseStatusCode() == 200)
textCompare.Clear()
textCompare.SetString(rest.responseHdrByName("x-ms-meta-upload-id"))
sameText7 = textCompare.ContentsEqual(uploadId,true)
textCompare.Clear()
textCompare.SetString(rest.responseHdrByName("x-ms-meta-source-sha256"))
sameText8 = textCompare.ContentsEqual(fingerprint,true)
textCompare.Clear()
textCompare.SetString(rest.responseHdrByName("Content-Length"))
sameText9 = textCompare.ContentsEqual(sourceSize,true)
if ((sameText7 != true) or (sameText8 != true) or (sameText9 != true))
print "Destination exists and is not this completed upload. Refusing to replace it." + "\n";
input.FileClose()
exit
end
print "Upload already committed. Keep the checkpoint until the application records completion." + "\n";
input.FileClose()
exit
end
textCompare.Clear()
textCompare.SetString(rest.responseHdrByName("x-ms-error-code"))
sameText10 = textCompare.ContentsEqual("BlobNotFound",true)
if ((rest.get_ResponseStatusCode() != 404) or (sameText10 != true))
print "Expected an absent blob in an existing container." + "\n";
input.FileClose()
exit
end
success = rest.AddQueryParam("comp","blocklist")
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = rest.AddQueryParam("blocklisttype","uncommitted")
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
rest.Disconnect(10)
success = connection.Connect(endpoint.getAsString(),443,true,20000)
if (success != true)
print "TLS reconnection failed." + "\n";
input.FileClose()
exit
end
success = rest.UseConnection(connection,false)
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
responseText = rest.fullRequestNoBody("GET",resourcePath)
if (rest.get_LastMethodSuccess() != true)
print "Could not reconcile uploaded blocks." + "\n";
input.FileClose()
exit
end
remoteBlocks = Chilkat::CkHashtable.new()
if (rest.get_ResponseStatusCode() == 200)
success = result.LoadXml(responseText)
if (success != true)
print "Expected well-formed XML." + "\n";
input.FileClose()
exit
end
if (result.TagEquals("BlockList") != true)
print "Unexpected XML response root." + "\n";
input.FileClose()
exit
end
remoteCount = result.NumChildrenAt("UncommittedBlocks")
j = 0
while (j < remoteCount)
result.put_I(j)
remoteId = result.getChildContent("UncommittedBlocks|Block[i]|Name")
remoteSize = result.getChildContent("UncommittedBlocks|Block[i]|Size")
textCompare.Clear()
textCompare.SetString(remoteId)
sameText11 = textCompare.ContentsEqual("",true)
textCompare.Clear()
textCompare.SetString(remoteSize)
sameText12 = textCompare.ContentsEqual("",true)
if ((sameText11 == true) or (sameText12 == true))
print "Malformed remote block listing." + "\n";
input.FileClose()
exit
end
success = remoteBlocks.AddStr(remoteId,remoteSize)
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
j = j + 1
end
else
textCompare.Clear()
textCompare.SetString(rest.responseHdrByName("x-ms-error-code"))
sameText13 = textCompare.ContentsEqual("BlobNotFound",true)
if ((rest.get_ResponseStatusCode() != 404) or (sameText13 != true))
print "Block listing failed." + "\n";
input.FileClose()
exit
end
end
blocks = Chilkat::CkXml.new()
blocks.put_Tag("BlockList")
chunk = Chilkat::CkBinData.new()
blockText = Chilkat::CkStringBuilder.new()
sizeText = Chilkat::CkStringBuilder.new()
i = 0
while (i < numBlocks)
success = chunk.Clear()
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = input.ReadBlockBd(i,blockSize,chunk)
if (success != true)
print "Could not read source block." + "\n";
input.FileClose()
exit
end
if ((chunk.get_NumBytes() < 1) or ((i < numBlocks - 1) and (chunk.get_NumBytes() != blockSize)))
print "Source changed or an incomplete read occurred." + "\n";
input.FileClose()
exit
end
blockText.Clear()
success = blockText.Append(uploadId)
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = blockText.Append(input.genBlockId(i,5,"ascii"))
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = blockText.Encode("base64","utf-8")
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
blockId = blockText.getAsString()
sizeText.Clear()
success = sizeText.AppendInt(chunk.get_NumBytes())
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
uploadBlock = true
if (remoteBlocks.Contains(blockId) == true)
if (remoteBlocks.lookupStr(blockId) != sizeText.getAsString())
print "Remote block size differs; do not commit this upload." + "\n";
input.FileClose()
exit
end
uploadBlock = false
end
if (uploadBlock == true)
success = rest.ClearAllQueryParams()
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = rest.AddQueryParam("comp","block")
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
success = rest.AddQueryParam("blockid",blockId)
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
rest.Disconnect(10)
success = connection.Connect(endpoint.getAsString(),443,true,20000)
if (success != true)
print "TLS reconnection failed." + "\n";
input.FileClose()
exit
end
success = rest.UseConnection(connection,false)
if (success != true)
print "Could not prepare the request." + "\n";
input.FileClose()
exit
end
responseBody.Clear()
success = rest.FullRequestBd("PUT",resourcePath,chunk,responseBody)
if (success != true)
print "Request did not complete; outcome may be uncertain." + "\n";
input.FileClose()
exit
end
if (rest.get_ResponseStatusCode() != 201)
print "HTTP status: " + rest.get_ResponseStatusCode().to_s() + "\n";
print "Azure error code: " + rest.responseHdrByName("x-ms-error-code") + "\n";
print "Request ID: " + rest.responseHdrByName("x-ms-request-id") + "\n";
# Inspect any error body privately; do not log credentials, SAS URLs or payloads.
input.FileClose()
exit
end
end
blocks.NewChild2("Uncommitted",blockId)
i = i + 1
end
# Rehash before commit. Do not modify the source or run concurrent writers.
finalFingerprint = hash.hashFileENC(sourceFile)
textCompare.Clear()
textCompare.SetString(finalFingerprint)
sameText14 = textCompare.ContentsEqual(fingerprint,true)
if ((hash.get_LastMethodSuccess() != true) or (sameText14 != true))
print "Source changed during staging; commit cancelled." + "\n";
input.FileClose()
exit
end
input.FileClose()
success = rest.ClearAllQueryParams()
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = rest.AddQueryParam("comp","blocklist")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = rest.AddHeader("Content-Type","application/xml")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = rest.AddHeader("x-ms-blob-content-type","application/octet-stream")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = rest.AddHeader("If-None-Match","*")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = rest.AddHeader("x-ms-meta-upload-id",uploadId)
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = rest.AddHeader("x-ms-meta-source-sha256",fingerprint)
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
rest.Disconnect(10)
success = connection.Connect(endpoint.getAsString(),443,true,20000)
if (success != true)
print "TLS reconnection failed." + "\n";
exit
end
success = rest.UseConnection(connection,false)
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
responseText = rest.fullRequestString("PUT",resourcePath,blocks.getXml())
if (rest.get_LastMethodSuccess() != true)
print "Request did not complete; outcome may be uncertain." + "\n";
exit
end
if (rest.get_ResponseStatusCode() != 201)
print "HTTP status: " + rest.get_ResponseStatusCode().to_s() + "\n";
print "Azure error code: " + rest.responseHdrByName("x-ms-error-code") + "\n";
print "Request ID: " + rest.responseHdrByName("x-ms-request-id") + "\n";
# Inspect any error body privately; do not log credentials, SAS URLs or payloads.
exit
end
print "Upload committed. Retain the checkpoint for reconciliation until completion is recorded." + "\n";