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 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 result = Server.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 = Server.CreateObject("Chilkat.FileAccess")
If (files.FileExists(finalFile) = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Choose a new final filename; existing output is preserved.") & "</pre>"
Response.End
End If
responseText = rest.FullRequestNoBody("HEAD",resourcePath)
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 <> 200) 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
etag = rest.ResponseHdrByName("ETag")
textCompare.Clear
success = textCompare.SetString(etag)
sameText3 = textCompare.ContentsEqual("",1)
If (sameText3 = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing blob ETag.") & "</pre>"
Response.End
End If
totalText = rest.ResponseHdrByName("Content-Length")
set totalValue = Server.CreateObject("Chilkat.StringBuilder")
success = totalValue.Append(totalText)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
If ((totalValue.Length < 1) Or (totalValue.Length > 10)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid or oversized length for this bounded example.") & "</pre>"
Response.End
End If
set totalDigits = Server.CreateObject("Chilkat.StringBuilder")
success = totalDigits.Append(totalText)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
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
Response.Write "<pre>" & Server.HTMLEncode( "Length must be a decimal string.") & "</pre>"
Response.End
End If
If ((totalValue.Length = 10) And (totalValue.StartsWith("1",1) <> 1) And (totalValue.ContentsEqual("2000000000",1) <> 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Length exceeds the 2,000,000,000-byte demonstration bound.") & "</pre>"
Response.End
End If
set totalNumber = Server.CreateObject("Chilkat.JsonObject")
success = totalNumber.UpdateNumber("value",totalText)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
total = totalNumber.IntOf("value")
set checkpoint = Server.CreateObject("Chilkat.JsonObject")
checkpoint.Strict = 1
If (files.FileExists(checkpointFile) = 1) Then
success = checkpoint.LoadFile(checkpointFile)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid checkpoint.") & "</pre>"
Response.End
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
Response.Write "<pre>" & Server.HTMLEncode( "The remote blob or checkpoint target changed. Preserve the old partial output and restart separately.") & "</pre>"
Response.End
End If
Else
If (files.FileExists(partFile) = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Partial file exists without its checkpoint; refusing to append.") & "</pre>"
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>"
Response.End
End If
success = checkpoint.UpdateString("path",resourcePath)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = checkpoint.UpdateString("etag",etag)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = checkpoint.UpdateString("size",totalText)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = checkpoint.WriteFile("qa_data/azure/download-checkpoint.tmp")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not write checkpoint.") & "</pre>"
Response.End
End If
success = files.FileRename("qa_data/azure/download-checkpoint.tmp",checkpointFile)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not publish checkpoint.") & "</pre>"
Response.End
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
Response.Write "<pre>" & Server.HTMLEncode( "Could not get partial-file size.") & "</pre>"
Response.End
End If
End If
offsetText = localSize
set offsetValue = Server.CreateObject("Chilkat.StringBuilder")
success = offsetValue.Append(offsetText)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
If ((offsetValue.Length < 1) Or (offsetValue.Length > 10)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid or oversized length for this bounded example.") & "</pre>"
Response.End
End If
set offsetDigits = Server.CreateObject("Chilkat.StringBuilder")
success = offsetDigits.Append(offsetText)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
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
Response.Write "<pre>" & Server.HTMLEncode( "Length must be a decimal string.") & "</pre>"
Response.End
End If
If ((offsetValue.Length = 10) And (offsetValue.StartsWith("1",1) <> 1) And (offsetValue.ContentsEqual("2000000000",1) <> 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Length exceeds the 2,000,000,000-byte demonstration bound.") & "</pre>"
Response.End
End If
set offsetNumber = Server.CreateObject("Chilkat.JsonObject")
success = offsetNumber.UpdateNumber("value",offsetText)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
offset = offsetNumber.IntOf("value")
If (offset > total) Then
Response.Write "<pre>" & Server.HTMLEncode( "Partial file is longer than the selected blob.") & "</pre>"
Response.End
End If
' Ensure an empty blob also creates a local file.
success = files.OpenForAppend(partFile)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not open partial file.") & "</pre>"
Response.End
End If
files.FileClose
success = rest.AddHeader("If-Match",etag)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
set chunk = Server.CreateObject("Chilkat.BinData")
set range = Server.CreateObject("Chilkat.StringBuilder")
set expectedRange = Server.CreateObject("Chilkat.StringBuilder")
set expectedLength = Server.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
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = range.AppendInt(offset)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = range.Append("-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = range.AppendInt(endByte)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = rest.AddHeader("x-ms-range",range.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
expectedRange.Clear
success = expectedRange.Append("bytes ")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = expectedRange.AppendInt(offset)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = expectedRange.Append("-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = expectedRange.AppendInt(endByte)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = expectedRange.Append("/")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = expectedRange.Append(totalText)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
expectedLength.Clear
success = expectedLength.AppendInt(count)
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
success = rest.SendReqNoBody("GET",resourcePath)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
httpStatus = rest.ReadResponseHeader()
If (httpStatus < 0) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read range response headers.") & "</pre>"
Response.End
End If
If (rest.ResponseStatusCode <> 206) 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
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
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected range response; no bytes appended.") & "</pre>"
Response.End
End If
success = chunk.Clear()
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not prepare the request.") & "</pre>"
Response.End
End If
success = rest.ReadRespBd(chunk)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Incomplete range body; no bytes appended.") & "</pre>"
Response.End
End If
If (chunk.NumBytes <> count) Then
Response.Write "<pre>" & Server.HTMLEncode( "Wrong byte count; no bytes appended.") & "</pre>"
Response.End
End If
success = files.OpenForAppend(partFile)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not reopen partial file.") & "</pre>"
Response.End
End If
success = files.FileWriteBd(chunk,0,0)
files.FileClose
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Local write failed. Inspect the partial file before retrying.") & "</pre>"
Response.End
End If
offset = offset + count
Loop
success = files.FileRename(partFile,finalFile)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not publish completed download.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Download complete. Retain or archive the checkpoint with the completed file.") & "</pre>"
%>
</body>
</html>