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

Azure Files: Download to a File with Streaming

See more Azure Cloud Storage Examples

Read response headers before writing bytes. Only HTTP 200 is streamed into a staging file, then renamed after completion. The final filename must not already exist; create qa_output first. An interrupted transfer can leave a .part file. This is a fresh download, not a resumable transfer.

Uses direct Chilkat REST calls with HTTPS certificate verification. Shared Key examples load a private qa_data/azure/storage.json file containing string fields account and access_key (the base64 account key). SAS consumers need only account there. Create local input/output directories before running. Examples target Azure public-cloud endpoints; adapt hostnames for other clouds or custom endpoints.

REST examples pin x-ms-version: 2026-04-06, and SAS generation uses the same service version. Complete-URL downloads use the supplied SAS version. This is a fully deployed version documented by Microsoft at review time. Older supported versions are not automatically obsolete. Transport completion and HTTP success are checked separately; writes are not automatically replayed after a lost response. Keep diagnostic dumps, keys and SAS URLs private.

Shared Key remains supported where allowed by account policy. Microsoft recommends Microsoft Entra ID, preferably managed identities where available, and user delegation SAS for delegated Blob access. Account-key SAS and Shared Key requests can be rejected when Shared Key access is disabled; these samples do not bypass that policy. See Azure data authorization.

Microsoft get file reference.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJsonObject.pb"
IncludeFile "CkSocket.pb"
IncludeFile "CkFileAccess.pb"
IncludeFile "CkAuthAzureStorage.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkStream.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  StringBuilder compares text by value in every generated language.
    textCompare.i = CkStringBuilder::ckCreate()
    If textCompare.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = 0
    config.i = CkJsonObject::ckCreate()
    If config.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(config, 1)
    success = CkJsonObject::ckLoadFile(config,"qa_data/azure/storage.json")
    If success <> 1
        Debug "Could not load private Azure configuration."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        ProcedureReturn
    EndIf

    CkStringBuilder::ckClear(textCompare)
    CkStringBuilder::ckSetString(textCompare,CkJsonObject::ckStringOf(config,"account"))
    sameText1.i = CkStringBuilder::ckContentsEqual(textCompare,"",1)
    If (CkJsonObject::ckJsonTypeOf(config,"account") <> 1) OR (sameText1 = 1)
        Debug "Supply the storage account name."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        ProcedureReturn
    EndIf

    ;  access_key is the base64 account key, not a connection string or SAS.
    CkStringBuilder::ckClear(textCompare)
    CkStringBuilder::ckSetString(textCompare,CkJsonObject::ckStringOf(config,"access_key"))
    sameText2.i = CkStringBuilder::ckContentsEqual(textCompare,"",1)
    If (CkJsonObject::ckJsonTypeOf(config,"access_key") <> 1) OR (sameText2 = 1)
        Debug "Supply the base64 account access key."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        ProcedureReturn
    EndIf

    endpoint.i = CkStringBuilder::ckCreate()
    If endpoint.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckAppend(endpoint,CkJsonObject::ckStringOf(config,"account"))
    If success <> 1
        Debug "Could not prepare the request."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        CkStringBuilder::ckDispose(endpoint)
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckAppend(endpoint,".file.core.windows.net")
    If success <> 1
        Debug "Could not prepare the request."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        CkStringBuilder::ckDispose(endpoint)
        ProcedureReturn
    EndIf

    ;  resourcePath is already URL-encoded. Encode each path segment once when adapting it.
    ;  Keep slash separators; names and paths are case-sensitive.
    resourcePath.s = "/examples/files/example.bin"
    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkRest::setCkConnectTimeoutMs(rest, 20000)
    CkRest::setCkIdleTimeoutMs(rest, 60000)
    azAuth.i = CkAuthAzureStorage::ckCreate()
    If azAuth.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthAzureStorage::setCkAccount(azAuth, CkJsonObject::ckStringOf(config,"account"))
    CkAuthAzureStorage::setCkAccessKey(azAuth, CkJsonObject::ckStringOf(config,"access_key"))
    CkAuthAzureStorage::setCkService(azAuth, "File")
    CkAuthAzureStorage::setCkScheme(azAuth, "SharedKey")
    CkAuthAzureStorage::setCkXMsVersion(azAuth, "2026-04-06")
    success = CkRest::ckSetAuthAzureStorage(rest,azAuth)
    If success <> 1
        Debug "Could not configure Shared Key signing."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        CkStringBuilder::ckDispose(endpoint)
        CkRest::ckDispose(rest)
        CkAuthAzureStorage::ckDispose(azAuth)
        ProcedureReturn
    EndIf

    connection.i = CkSocket::ckCreate()
    If connection.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSocket::setCkRequireSslCertVerify(connection, 1)
    CkSocket::setCkMaxReadIdleMs(connection, 60000)
    CkSocket::setCkMaxSendIdleMs(connection, 60000)
    success = CkSocket::ckConnect(connection,CkStringBuilder::ckGetAsString(endpoint),443,1,20000)
    If success <> 1
        Debug "TLS connection failed; inspect diagnostics privately."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        CkStringBuilder::ckDispose(endpoint)
        CkRest::ckDispose(rest)
        CkAuthAzureStorage::ckDispose(azAuth)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    ;  Do not replay a write automatically after losing its response.
    success = CkRest::ckUseConnection(rest,connection,0)
    If success <> 1
        Debug "Could not prepare the request."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        CkStringBuilder::ckDispose(endpoint)
        CkRest::ckDispose(rest)
        CkAuthAzureStorage::ckDispose(azAuth)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    result.i = CkXml::ckCreate()
    If result.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkStrict(result, 1)
    ;  Use a new destination and a staging file. A failed transfer is never a completed output.
    files.i = CkFileAccess::ckCreate()
    If files.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    If CkFileAccess::ckFileExists(files,"qa_output/example.bin") = 1
        Debug "Choose a new output filename."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        CkStringBuilder::ckDispose(endpoint)
        CkRest::ckDispose(rest)
        CkAuthAzureStorage::ckDispose(azAuth)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(result)
        CkFileAccess::ckDispose(files)
        ProcedureReturn
    EndIf

    sink.i = CkStream::ckCreate()
    If sink.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStream::setCkSinkFile(sink, "qa_output/example.bin.part")
    success = CkRest::ckSendReqNoBody(rest,"GET",resourcePath)
    If success <> 1
        Debug "Could not send download request."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        CkStringBuilder::ckDispose(endpoint)
        CkRest::ckDispose(rest)
        CkAuthAzureStorage::ckDispose(azAuth)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(result)
        CkFileAccess::ckDispose(files)
        CkStream::ckDispose(sink)
        ProcedureReturn
    EndIf

    httpStatus.i = CkRest::ckReadResponseHeader(rest)
    If httpStatus < 0
        Debug "Could not read response headers."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        CkStringBuilder::ckDispose(endpoint)
        CkRest::ckDispose(rest)
        CkAuthAzureStorage::ckDispose(azAuth)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(result)
        CkFileAccess::ckDispose(files)
        CkStream::ckDispose(sink)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "HTTP status: " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "Azure error code: " + CkRest::ckResponseHdrByName(rest,"x-ms-error-code")
        Debug "Request ID: " + CkRest::ckResponseHdrByName(rest,"x-ms-request-id")
        ;  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        CkStringBuilder::ckDispose(endpoint)
        CkRest::ckDispose(rest)
        CkAuthAzureStorage::ckDispose(azAuth)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(result)
        CkFileAccess::ckDispose(files)
        CkStream::ckDispose(sink)
        ProcedureReturn
    EndIf

    success = CkRest::ckReadRespBodyStream(rest,sink,0)
    If success <> 1
        Debug "Download incomplete; staging file is not a valid final output."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        CkStringBuilder::ckDispose(endpoint)
        CkRest::ckDispose(rest)
        CkAuthAzureStorage::ckDispose(azAuth)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(result)
        CkFileAccess::ckDispose(files)
        CkStream::ckDispose(sink)
        ProcedureReturn
    EndIf

    success = CkFileAccess::ckFileRename(files,"qa_output/example.bin.part","qa_output/example.bin")
    If success <> 1
        Debug "Could not publish the downloaded file."
        CkStringBuilder::ckDispose(textCompare)
        CkJsonObject::ckDispose(config)
        CkStringBuilder::ckDispose(endpoint)
        CkRest::ckDispose(rest)
        CkAuthAzureStorage::ckDispose(azAuth)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(result)
        CkFileAccess::ckDispose(files)
        CkStream::ckDispose(sink)
        ProcedureReturn
    EndIf

    Debug "Download completed to qa_output/example.bin."


    CkStringBuilder::ckDispose(textCompare)
    CkJsonObject::ckDispose(config)
    CkStringBuilder::ckDispose(endpoint)
    CkRest::ckDispose(rest)
    CkAuthAzureStorage::ckDispose(azAuth)
    CkSocket::ckDispose(connection)
    CkXml::ckDispose(result)
    CkFileAccess::ckDispose(files)
    CkStream::ckDispose(sink)


    ProcedureReturn
EndProcedure