Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.6.0+

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.

Microsoft reference.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loTextCompare
LOCAL loConfig
LOCAL lnSameText1
LOCAL lnSameText2
LOCAL loEndpoint
LOCAL lcResourcePath
LOCAL loRest
LOCAL loAzAuth
LOCAL loConnection
LOCAL lcResponseText
LOCAL loResult
LOCAL lcPartFile
LOCAL lcFinalFile
LOCAL lcCheckpointFile
LOCAL loFiles
LOCAL lcEtag
LOCAL lnSameText3
LOCAL lcTotalText
LOCAL loTotalValue
LOCAL loTotalDigits
LOCAL lnTotalRemoved
LOCAL loTotalNumber
LOCAL lnTotal
LOCAL loCheckpoint
LOCAL lnSameText4
LOCAL lnSameText5
LOCAL lnSameText6
LOCAL lcLocalSize
LOCAL lcOffsetText
LOCAL loOffsetValue
LOCAL loOffsetDigits
LOCAL lnOffsetRemoved
LOCAL loOffsetNumber
LOCAL lnOffset
LOCAL loChunk
LOCAL loRange
LOCAL loExpectedRange
LOCAL loExpectedLength
LOCAL lnCount
LOCAL lnEndByte
LOCAL lnHttpStatus
LOCAL lnSameText7

lnSuccess = 0

*  StringBuilder compares text by value in every generated language.
loTextCompare = CreateObject('Chilkat.StringBuilder')
*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loConfig = CreateObject('Chilkat.JsonObject')
loConfig.Strict = 1
lnSuccess = loConfig.LoadFile("qa_data/azure/storage.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load private Azure configuration."
    RELEASE loTextCompare
    RELEASE loConfig
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loConfig.StringOf("account"))
lnSameText1 = loTextCompare.ContentsEqual("",1)
IF ((loConfig.JsonTypeOf("account") <> 1) OR (lnSameText1 = 1)) THEN
    ? "Supply the storage account name."
    RELEASE loTextCompare
    RELEASE loConfig
    CANCEL
ENDIF

*  access_key is the base64 account key, not a connection string or SAS.
loTextCompare.Clear()
loTextCompare.SetString(loConfig.StringOf("access_key"))
lnSameText2 = loTextCompare.ContentsEqual("",1)
IF ((loConfig.JsonTypeOf("access_key") <> 1) OR (lnSameText2 = 1)) THEN
    ? "Supply the base64 account access key."
    RELEASE loTextCompare
    RELEASE loConfig
    CANCEL
ENDIF

loEndpoint = CreateObject('Chilkat.StringBuilder')
lnSuccess = loEndpoint.Append(loConfig.StringOf("account"))
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    CANCEL
ENDIF

lnSuccess = loEndpoint.Append(".blob.core.windows.net")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    CANCEL
ENDIF

*  resourcePath is already URL-encoded. Encode each path segment once when adapting it.
*  Keep slash separators; names and paths are case-sensitive.
lcResourcePath = "/examplecontainer/example.bin"
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
loAzAuth = CreateObject('Chilkat.AuthAzureStorage')
loAzAuth.Account = loConfig.StringOf("account")
loAzAuth.AccessKey = loConfig.StringOf("access_key")
loAzAuth.Service = "Blob"
loAzAuth.Scheme = "SharedKey"
loAzAuth.XMsVersion = "2026-04-06"
lnSuccess = loRest.SetAuthAzureStorage(loAzAuth)
IF (lnSuccess <> 1) THEN
    ? "Could not configure Shared Key signing."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    CANCEL
ENDIF

loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
    ? "TLS connection failed; inspect diagnostics privately."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    CANCEL
ENDIF

*  Do not replay a write automatically after losing its response.
lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    CANCEL
ENDIF

loResult = CreateObject('Chilkat.Xml')
loResult.Strict = 1
lcPartFile = "qa_output/resume.bin.part"
lcFinalFile = "qa_output/resume.bin"
lcCheckpointFile = "qa_data/azure/download-checkpoint.json"
loFiles = CreateObject('Chilkat.FileAccess')
IF (loFiles.FileExists(lcFinalFile) = 1) THEN
    ? "Choose a new final filename; existing output is preserved."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    CANCEL
ENDIF

lcResponseText = loRest.FullRequestNoBody("HEAD",lcResourcePath)
IF (loRest.LastMethodSuccess <> 1) THEN
    ? "Request did not complete; outcome may be uncertain."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode <> 200) THEN
    ? "HTTP status: " + STR(loRest.ResponseStatusCode)
    ? "Azure error code: " + loRest.ResponseHdrByName("x-ms-error-code")
    ? "Request ID: " + loRest.ResponseHdrByName("x-ms-request-id")
    *  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    CANCEL
ENDIF

lcEtag = loRest.ResponseHdrByName("ETag")
loTextCompare.Clear()
loTextCompare.SetString(lcEtag)
lnSameText3 = loTextCompare.ContentsEqual("",1)
IF (lnSameText3 = 1) THEN
    ? "Missing blob ETag."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    CANCEL
ENDIF

lcTotalText = loRest.ResponseHdrByName("Content-Length")
loTotalValue = CreateObject('Chilkat.StringBuilder')
lnSuccess = loTotalValue.Append(lcTotalText)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    CANCEL
ENDIF

IF ((loTotalValue.Length < 1) OR (loTotalValue.Length > 10)) THEN
    ? "Invalid or oversized length for this bounded example."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    CANCEL
ENDIF

loTotalDigits = CreateObject('Chilkat.StringBuilder')
lnSuccess = loTotalDigits.Append(lcTotalText)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    CANCEL
ENDIF

lnTotalRemoved = 0
lnTotalRemoved = loTotalDigits.Replace("0","")
lnTotalRemoved = loTotalDigits.Replace("1","")
lnTotalRemoved = loTotalDigits.Replace("2","")
lnTotalRemoved = loTotalDigits.Replace("3","")
lnTotalRemoved = loTotalDigits.Replace("4","")
lnTotalRemoved = loTotalDigits.Replace("5","")
lnTotalRemoved = loTotalDigits.Replace("6","")
lnTotalRemoved = loTotalDigits.Replace("7","")
lnTotalRemoved = loTotalDigits.Replace("8","")
lnTotalRemoved = loTotalDigits.Replace("9","")
IF (loTotalDigits.Length <> 0) THEN
    ? "Length must be a decimal string."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    CANCEL
ENDIF

IF ((loTotalValue.Length = 10) AND (loTotalValue.StartsWith("1",1) <> 1) AND (loTotalValue.ContentsEqual("2000000000",1) <> 1)) THEN
    ? "Length exceeds the 2,000,000,000-byte demonstration bound."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    CANCEL
ENDIF

loTotalNumber = CreateObject('Chilkat.JsonObject')
lnSuccess = loTotalNumber.UpdateNumber("value",lcTotalText)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    RELEASE loTotalNumber
    CANCEL
ENDIF

lnTotal = loTotalNumber.IntOf("value")
loCheckpoint = CreateObject('Chilkat.JsonObject')
loCheckpoint.Strict = 1
IF (loFiles.FileExists(lcCheckpointFile) = 1) THEN
    lnSuccess = loCheckpoint.LoadFile(lcCheckpointFile)
    IF (lnSuccess <> 1) THEN
        ? "Invalid checkpoint."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        CANCEL
    ENDIF

    loTextCompare.Clear()
    loTextCompare.SetString(loCheckpoint.StringOf("path"))
    lnSameText4 = loTextCompare.ContentsEqual(lcResourcePath,1)
    loTextCompare.Clear()
    loTextCompare.SetString(loCheckpoint.StringOf("etag"))
    lnSameText5 = loTextCompare.ContentsEqual(lcEtag,1)
    loTextCompare.Clear()
    loTextCompare.SetString(loCheckpoint.StringOf("size"))
    lnSameText6 = loTextCompare.ContentsEqual(lcTotalText,1)
    IF ((loCheckpoint.StringOf("account") <> loConfig.StringOf("account")) OR (lnSameText4 <> 1) OR (lnSameText5 <> 1) OR (lnSameText6 <> 1)) THEN
        ? "The remote blob or checkpoint target changed. Preserve the old partial output and restart separately."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        CANCEL
    ENDIF

ELSE
    IF (loFiles.FileExists(lcPartFile) = 1) THEN
        ? "Partial file exists without its checkpoint; refusing to append."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        CANCEL
    ENDIF

    lnSuccess = loCheckpoint.UpdateString("account",loConfig.StringOf("account"))
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        CANCEL
    ENDIF

    lnSuccess = loCheckpoint.UpdateString("path",lcResourcePath)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        CANCEL
    ENDIF

    lnSuccess = loCheckpoint.UpdateString("etag",lcEtag)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        CANCEL
    ENDIF

    lnSuccess = loCheckpoint.UpdateString("size",lcTotalText)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        CANCEL
    ENDIF

    lnSuccess = loCheckpoint.WriteFile("qa_data/azure/download-checkpoint.tmp")
    IF (lnSuccess <> 1) THEN
        ? "Could not write checkpoint."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        CANCEL
    ENDIF

    lnSuccess = loFiles.FileRename("qa_data/azure/download-checkpoint.tmp",lcCheckpointFile)
    IF (lnSuccess <> 1) THEN
        ? "Could not publish checkpoint."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        CANCEL
    ENDIF

ENDIF

*  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.
lcLocalSize = "0"
IF (loFiles.FileExists(lcPartFile) = 1) THEN
    lcLocalSize = loFiles.FileSizeStr(lcPartFile)
    IF (loFiles.LastMethodSuccess <> 1) THEN
        ? "Could not get partial-file size."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        CANCEL
    ENDIF

ENDIF

lcOffsetText = lcLocalSize
loOffsetValue = CreateObject('Chilkat.StringBuilder')
lnSuccess = loOffsetValue.Append(lcOffsetText)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    RELEASE loTotalNumber
    RELEASE loCheckpoint
    RELEASE loOffsetValue
    CANCEL
ENDIF

IF ((loOffsetValue.Length < 1) OR (loOffsetValue.Length > 10)) THEN
    ? "Invalid or oversized length for this bounded example."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    RELEASE loTotalNumber
    RELEASE loCheckpoint
    RELEASE loOffsetValue
    CANCEL
ENDIF

loOffsetDigits = CreateObject('Chilkat.StringBuilder')
lnSuccess = loOffsetDigits.Append(lcOffsetText)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    RELEASE loTotalNumber
    RELEASE loCheckpoint
    RELEASE loOffsetValue
    RELEASE loOffsetDigits
    CANCEL
ENDIF

lnOffsetRemoved = 0
lnOffsetRemoved = loOffsetDigits.Replace("0","")
lnOffsetRemoved = loOffsetDigits.Replace("1","")
lnOffsetRemoved = loOffsetDigits.Replace("2","")
lnOffsetRemoved = loOffsetDigits.Replace("3","")
lnOffsetRemoved = loOffsetDigits.Replace("4","")
lnOffsetRemoved = loOffsetDigits.Replace("5","")
lnOffsetRemoved = loOffsetDigits.Replace("6","")
lnOffsetRemoved = loOffsetDigits.Replace("7","")
lnOffsetRemoved = loOffsetDigits.Replace("8","")
lnOffsetRemoved = loOffsetDigits.Replace("9","")
IF (loOffsetDigits.Length <> 0) THEN
    ? "Length must be a decimal string."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    RELEASE loTotalNumber
    RELEASE loCheckpoint
    RELEASE loOffsetValue
    RELEASE loOffsetDigits
    CANCEL
ENDIF

IF ((loOffsetValue.Length = 10) AND (loOffsetValue.StartsWith("1",1) <> 1) AND (loOffsetValue.ContentsEqual("2000000000",1) <> 1)) THEN
    ? "Length exceeds the 2,000,000,000-byte demonstration bound."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    RELEASE loTotalNumber
    RELEASE loCheckpoint
    RELEASE loOffsetValue
    RELEASE loOffsetDigits
    CANCEL
ENDIF

loOffsetNumber = CreateObject('Chilkat.JsonObject')
lnSuccess = loOffsetNumber.UpdateNumber("value",lcOffsetText)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    RELEASE loTotalNumber
    RELEASE loCheckpoint
    RELEASE loOffsetValue
    RELEASE loOffsetDigits
    RELEASE loOffsetNumber
    CANCEL
ENDIF

lnOffset = loOffsetNumber.IntOf("value")
IF (lnOffset > lnTotal) THEN
    ? "Partial file is longer than the selected blob."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    RELEASE loTotalNumber
    RELEASE loCheckpoint
    RELEASE loOffsetValue
    RELEASE loOffsetDigits
    RELEASE loOffsetNumber
    CANCEL
ENDIF

*  Ensure an empty blob also creates a local file.
lnSuccess = loFiles.OpenForAppend(lcPartFile)
IF (lnSuccess <> 1) THEN
    ? "Could not open partial file."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    RELEASE loTotalNumber
    RELEASE loCheckpoint
    RELEASE loOffsetValue
    RELEASE loOffsetDigits
    RELEASE loOffsetNumber
    CANCEL
ENDIF

loFiles.FileClose()
lnSuccess = loRest.AddHeader("If-Match",lcEtag)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    RELEASE loTotalNumber
    RELEASE loCheckpoint
    RELEASE loOffsetValue
    RELEASE loOffsetDigits
    RELEASE loOffsetNumber
    CANCEL
ENDIF

loChunk = CreateObject('Chilkat.BinData')
loRange = CreateObject('Chilkat.StringBuilder')
loExpectedRange = CreateObject('Chilkat.StringBuilder')
loExpectedLength = CreateObject('Chilkat.StringBuilder')
DO WHILE (lnOffset < lnTotal)
    lnCount = lnTotal - lnOffset
    IF (lnCount > 4194304) THEN
        lnCount = 4194304
    ENDIF

    lnEndByte = lnOffset + lnCount - 1
    loRange.Clear()
    lnSuccess = loRange.Append("bytes=")
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loRange.AppendInt(lnOffset)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loRange.Append("-")
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loRange.AppendInt(lnEndByte)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loRest.AddHeader("x-ms-range",loRange.GetAsString())
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    loExpectedRange.Clear()
    lnSuccess = loExpectedRange.Append("bytes ")
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loExpectedRange.AppendInt(lnOffset)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loExpectedRange.Append("-")
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loExpectedRange.AppendInt(lnEndByte)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loExpectedRange.Append("/")
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loExpectedRange.Append(lcTotalText)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    loExpectedLength.Clear()
    lnSuccess = loExpectedLength.AppendInt(lnCount)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    loRest.Disconnect(10)
    lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
    IF (lnSuccess <> 1) THEN
        ? "TLS reconnection failed."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loRest.UseConnection(loConnection,0)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loRest.SendReqNoBody("GET",lcResourcePath)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnHttpStatus = loRest.ReadResponseHeader()
    IF (lnHttpStatus < 0) THEN
        ? "Could not read range response headers."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    IF (loRest.ResponseStatusCode <> 206) THEN
        ? "HTTP status: " + STR(loRest.ResponseStatusCode)
        ? "Azure error code: " + loRest.ResponseHdrByName("x-ms-error-code")
        ? "Request ID: " + loRest.ResponseHdrByName("x-ms-request-id")
        *  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    loTextCompare.Clear()
    loTextCompare.SetString(loRest.ResponseHdrByName("ETag"))
    lnSameText7 = loTextCompare.ContentsEqual(lcEtag,1)
    IF ((lnSameText7 <> 1) OR (loRest.ResponseHdrByName("Content-Range") <> loExpectedRange.GetAsString()) OR (loRest.ResponseHdrByName("Content-Length") <> loExpectedLength.GetAsString())) THEN
        ? "Unexpected range response; no bytes appended."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loChunk.Clear()
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loRest.ReadRespBd(loChunk)
    IF (lnSuccess <> 1) THEN
        ? "Incomplete range body; no bytes appended."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    IF (loChunk.NumBytes <> lnCount) THEN
        ? "Wrong byte count; no bytes appended."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loFiles.OpenForAppend(lcPartFile)
    IF (lnSuccess <> 1) THEN
        ? "Could not reopen partial file."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnSuccess = loFiles.FileWriteBd(loChunk,0,0)
    loFiles.FileClose()
    IF (lnSuccess <> 1) THEN
        ? "Local write failed. Inspect the partial file before retrying."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loEndpoint
        RELEASE loRest
        RELEASE loAzAuth
        RELEASE loConnection
        RELEASE loResult
        RELEASE loFiles
        RELEASE loTotalValue
        RELEASE loTotalDigits
        RELEASE loTotalNumber
        RELEASE loCheckpoint
        RELEASE loOffsetValue
        RELEASE loOffsetDigits
        RELEASE loOffsetNumber
        RELEASE loChunk
        RELEASE loRange
        RELEASE loExpectedRange
        RELEASE loExpectedLength
        CANCEL
    ENDIF

    lnOffset = lnOffset + lnCount
ENDDO
lnSuccess = loFiles.FileRename(lcPartFile,lcFinalFile)
IF (lnSuccess <> 1) THEN
    ? "Could not publish completed download."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loAzAuth
    RELEASE loConnection
    RELEASE loResult
    RELEASE loFiles
    RELEASE loTotalValue
    RELEASE loTotalDigits
    RELEASE loTotalNumber
    RELEASE loCheckpoint
    RELEASE loOffsetValue
    RELEASE loOffsetDigits
    RELEASE loOffsetNumber
    RELEASE loChunk
    RELEASE loRange
    RELEASE loExpectedRange
    RELEASE loExpectedLength
    CANCEL
ENDIF

? "Download complete. Retain or archive the checkpoint with the completed file."

RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loFiles
RELEASE loTotalValue
RELEASE loTotalDigits
RELEASE loTotalNumber
RELEASE loCheckpoint
RELEASE loOffsetValue
RELEASE loOffsetDigits
RELEASE loOffsetNumber
RELEASE loChunk
RELEASE loRange
RELEASE loExpectedRange
RELEASE loExpectedLength