Azure Blob: Resume a Download with ETag Protection
See more Azure Cloud Storage Examples
Save the account, path, ETag and size in an immutable checkpoint, then append complete byte ranges to a private .part file. A later run uses the existing partial-file length as its offset and checks that HEAD still describes the same blob. Every range carries If-Match and must return the expected ETag, exact Content-Range, Content-Length and body size before any bytes are appended.
The demonstration bounds offsets to 2,000,000,000 bytes so it can use int arithmetic without overflow; this is not an Azure service limit. Sizes in checkpoints remain strings. Only the final successful transfer renames the partial output to the final filename. Empty blobs are handled without issuing an invalid range.
Use one process, an exclusive local destination and an unchanged partial file. The checkpoint does not independently hash or authenticate local content; do not point it at an unrelated file or reuse a partial file that was edited or suffered disk corruption. After a local write failure inspect or restart the partial file. On 412 or changed HEAD properties, preserve the previous partial output and start a new download separately instead of combining versions. A crash while initially publishing the checkpoint fails closed if state is incomplete.
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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' StringBuilder compares text by value in every generated language.
set textCompare = CreateObject("Chilkat.StringBuilder")
' Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
set config = CreateObject("Chilkat.JsonObject")
config.Strict = 1
success = config.LoadFile("qa_data/azure/storage.json")
If (success <> 1) Then
outFile.WriteLine("Could not load private Azure configuration.")
WScript.Quit
End If
textCompare.Clear
success = textCompare.SetString(config.StringOf("account"))
sameText1 = textCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("account") <> 1) Or (sameText1 = 1)) Then
outFile.WriteLine("Supply the storage account name.")
WScript.Quit
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
outFile.WriteLine("Supply the base64 account access key.")
WScript.Quit
End If
set endpoint = CreateObject("Chilkat.StringBuilder")
success = endpoint.Append(config.StringOf("account"))
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = endpoint.Append(".blob.core.windows.net")
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
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 = CreateObject("Chilkat.Rest")
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
set azAuth = 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
outFile.WriteLine("Could not configure Shared Key signing.")
WScript.Quit
End If
set connection = CreateObject("Chilkat.Socket")
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
outFile.WriteLine("TLS connection failed; inspect diagnostics privately.")
WScript.Quit
End If
' Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,0)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
set result = CreateObject("Chilkat.Xml")
result.Strict = 1
partFile = "qa_output/resume.bin.part"
finalFile = "qa_output/resume.bin"
checkpointFile = "qa_data/azure/download-checkpoint.json"
set files = CreateObject("Chilkat.FileAccess")
If (files.FileExists(finalFile) = 1) Then
outFile.WriteLine("Choose a new final filename; existing output is preserved.")
WScript.Quit
End If
responseText = rest.FullRequestNoBody("HEAD",resourcePath)
If (rest.LastMethodSuccess <> 1) Then
outFile.WriteLine("Request did not complete; outcome may be uncertain.")
WScript.Quit
End If
If (rest.ResponseStatusCode <> 200) Then
outFile.WriteLine("HTTP status: " & rest.ResponseStatusCode)
outFile.WriteLine("Azure error code: " & rest.ResponseHdrByName("x-ms-error-code"))
outFile.WriteLine("Request ID: " & rest.ResponseHdrByName("x-ms-request-id"))
' Inspect any error body privately; do not log credentials, SAS URLs or payloads.
WScript.Quit
End If
etag = rest.ResponseHdrByName("ETag")
textCompare.Clear
success = textCompare.SetString(etag)
sameText3 = textCompare.ContentsEqual("",1)
If (sameText3 = 1) Then
outFile.WriteLine("Missing blob ETag.")
WScript.Quit
End If
totalText = rest.ResponseHdrByName("Content-Length")
set totalValue = CreateObject("Chilkat.StringBuilder")
success = totalValue.Append(totalText)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
If ((totalValue.Length < 1) Or (totalValue.Length > 10)) Then
outFile.WriteLine("Invalid or oversized length for this bounded example.")
WScript.Quit
End If
set totalDigits = CreateObject("Chilkat.StringBuilder")
success = totalDigits.Append(totalText)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
totalRemoved = 0
totalRemoved = totalDigits.Replace("0","")
totalRemoved = totalDigits.Replace("1","")
totalRemoved = totalDigits.Replace("2","")
totalRemoved = totalDigits.Replace("3","")
totalRemoved = totalDigits.Replace("4","")
totalRemoved = totalDigits.Replace("5","")
totalRemoved = totalDigits.Replace("6","")
totalRemoved = totalDigits.Replace("7","")
totalRemoved = totalDigits.Replace("8","")
totalRemoved = totalDigits.Replace("9","")
If (totalDigits.Length <> 0) Then
outFile.WriteLine("Length must be a decimal string.")
WScript.Quit
End If
If ((totalValue.Length = 10) And (totalValue.StartsWith("1",1) <> 1) And (totalValue.ContentsEqual("2000000000",1) <> 1)) Then
outFile.WriteLine("Length exceeds the 2,000,000,000-byte demonstration bound.")
WScript.Quit
End If
set totalNumber = CreateObject("Chilkat.JsonObject")
success = totalNumber.UpdateNumber("value",totalText)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
total = totalNumber.IntOf("value")
set checkpoint = CreateObject("Chilkat.JsonObject")
checkpoint.Strict = 1
If (files.FileExists(checkpointFile) = 1) Then
success = checkpoint.LoadFile(checkpointFile)
If (success <> 1) Then
outFile.WriteLine("Invalid checkpoint.")
WScript.Quit
End If
textCompare.Clear
success = textCompare.SetString(checkpoint.StringOf("path"))
sameText4 = textCompare.ContentsEqual(resourcePath,1)
textCompare.Clear
success = textCompare.SetString(checkpoint.StringOf("etag"))
sameText5 = textCompare.ContentsEqual(etag,1)
textCompare.Clear
success = textCompare.SetString(checkpoint.StringOf("size"))
sameText6 = textCompare.ContentsEqual(totalText,1)
If ((checkpoint.StringOf("account") <> config.StringOf("account")) Or (sameText4 <> 1) Or (sameText5 <> 1) Or (sameText6 <> 1)) Then
outFile.WriteLine("The remote blob or checkpoint target changed. Preserve the old partial output and restart separately.")
WScript.Quit
End If
Else
If (files.FileExists(partFile) = 1) Then
outFile.WriteLine("Partial file exists without its checkpoint; refusing to append.")
WScript.Quit
End If
success = checkpoint.UpdateString("account",config.StringOf("account"))
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = checkpoint.UpdateString("path",resourcePath)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = checkpoint.UpdateString("etag",etag)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = checkpoint.UpdateString("size",totalText)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = checkpoint.WriteFile("qa_data/azure/download-checkpoint.tmp")
If (success <> 1) Then
outFile.WriteLine("Could not write checkpoint.")
WScript.Quit
End If
success = files.FileRename("qa_data/azure/download-checkpoint.tmp",checkpointFile)
If (success <> 1) Then
outFile.WriteLine("Could not publish checkpoint.")
WScript.Quit
End If
End If
' The immutable checkpoint identifies the version. Actual local length is the resume offset.
' Keep this partial file private, unchanged and exclusive to this one download.
localSize = "0"
If (files.FileExists(partFile) = 1) Then
localSize = files.FileSizeStr(partFile)
If (files.LastMethodSuccess <> 1) Then
outFile.WriteLine("Could not get partial-file size.")
WScript.Quit
End If
End If
offsetText = localSize
set offsetValue = CreateObject("Chilkat.StringBuilder")
success = offsetValue.Append(offsetText)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
If ((offsetValue.Length < 1) Or (offsetValue.Length > 10)) Then
outFile.WriteLine("Invalid or oversized length for this bounded example.")
WScript.Quit
End If
set offsetDigits = CreateObject("Chilkat.StringBuilder")
success = offsetDigits.Append(offsetText)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
offsetRemoved = 0
offsetRemoved = offsetDigits.Replace("0","")
offsetRemoved = offsetDigits.Replace("1","")
offsetRemoved = offsetDigits.Replace("2","")
offsetRemoved = offsetDigits.Replace("3","")
offsetRemoved = offsetDigits.Replace("4","")
offsetRemoved = offsetDigits.Replace("5","")
offsetRemoved = offsetDigits.Replace("6","")
offsetRemoved = offsetDigits.Replace("7","")
offsetRemoved = offsetDigits.Replace("8","")
offsetRemoved = offsetDigits.Replace("9","")
If (offsetDigits.Length <> 0) Then
outFile.WriteLine("Length must be a decimal string.")
WScript.Quit
End If
If ((offsetValue.Length = 10) And (offsetValue.StartsWith("1",1) <> 1) And (offsetValue.ContentsEqual("2000000000",1) <> 1)) Then
outFile.WriteLine("Length exceeds the 2,000,000,000-byte demonstration bound.")
WScript.Quit
End If
set offsetNumber = CreateObject("Chilkat.JsonObject")
success = offsetNumber.UpdateNumber("value",offsetText)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
offset = offsetNumber.IntOf("value")
If (offset > total) Then
outFile.WriteLine("Partial file is longer than the selected blob.")
WScript.Quit
End If
' Ensure an empty blob also creates a local file.
success = files.OpenForAppend(partFile)
If (success <> 1) Then
outFile.WriteLine("Could not open partial file.")
WScript.Quit
End If
files.FileClose
success = rest.AddHeader("If-Match",etag)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
set chunk = CreateObject("Chilkat.BinData")
set range = CreateObject("Chilkat.StringBuilder")
set expectedRange = CreateObject("Chilkat.StringBuilder")
set expectedLength = CreateObject("Chilkat.StringBuilder")
Do While (offset < total)
count = total - offset
If (count > 4194304) Then
count = 4194304
End If
endByte = offset + count - 1
range.Clear
success = range.Append("bytes=")
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = range.AppendInt(offset)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = range.Append("-")
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = range.AppendInt(endByte)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = rest.AddHeader("x-ms-range",range.GetAsString())
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
expectedRange.Clear
success = expectedRange.Append("bytes ")
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = expectedRange.AppendInt(offset)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = expectedRange.Append("-")
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = expectedRange.AppendInt(endByte)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = expectedRange.Append("/")
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = expectedRange.Append(totalText)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
expectedLength.Clear
success = expectedLength.AppendInt(count)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = rest.Disconnect(10)
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
outFile.WriteLine("TLS reconnection failed.")
WScript.Quit
End If
success = rest.UseConnection(connection,0)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = rest.SendReqNoBody("GET",resourcePath)
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
httpStatus = rest.ReadResponseHeader()
If (httpStatus < 0) Then
outFile.WriteLine("Could not read range response headers.")
WScript.Quit
End If
If (rest.ResponseStatusCode <> 206) Then
outFile.WriteLine("HTTP status: " & rest.ResponseStatusCode)
outFile.WriteLine("Azure error code: " & rest.ResponseHdrByName("x-ms-error-code"))
outFile.WriteLine("Request ID: " & rest.ResponseHdrByName("x-ms-request-id"))
' Inspect any error body privately; do not log credentials, SAS URLs or payloads.
WScript.Quit
End If
textCompare.Clear
success = textCompare.SetString(rest.ResponseHdrByName("ETag"))
sameText7 = textCompare.ContentsEqual(etag,1)
If ((sameText7 <> 1) Or (rest.ResponseHdrByName("Content-Range") <> expectedRange.GetAsString()) Or (rest.ResponseHdrByName("Content-Length") <> expectedLength.GetAsString())) Then
outFile.WriteLine("Unexpected range response; no bytes appended.")
WScript.Quit
End If
success = chunk.Clear()
If (success <> 1) Then
outFile.WriteLine("Could not prepare the request.")
WScript.Quit
End If
success = rest.ReadRespBd(chunk)
If (success <> 1) Then
outFile.WriteLine("Incomplete range body; no bytes appended.")
WScript.Quit
End If
If (chunk.NumBytes <> count) Then
outFile.WriteLine("Wrong byte count; no bytes appended.")
WScript.Quit
End If
success = files.OpenForAppend(partFile)
If (success <> 1) Then
outFile.WriteLine("Could not reopen partial file.")
WScript.Quit
End If
success = files.FileWriteBd(chunk,0,0)
files.FileClose
If (success <> 1) Then
outFile.WriteLine("Local write failed. Inspect the partial file before retrying.")
WScript.Quit
End If
offset = offset + count
Loop
success = files.FileRename(partFile,finalFile)
If (success <> 1) Then
outFile.WriteLine("Could not publish completed download.")
WScript.Quit
End If
outFile.WriteLine("Download complete. Retain or archive the checkpoint with the completed file.")
outFile.Close