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

Azure Files: Download with Entra OAuth and Backup Intent

See more Azure Cloud Storage Examples

Use a Storage-audience token saved by either authentication example, then stream a file through FileREST with x-ms-file-request-intent: backup. The identity needs file read permission and readFileBackupSemantics; Storage File Data Privileged Reader is the built-in read role for this use. This privileged access can bypass file/directory ACLs within the role's scope. Ordinary SMB Share Reader permissions alone are insufficient.

This is REST authorization, not SMB/Kerberos authentication. It applies to classic Azure file shares created through Microsoft.Storage, not Microsoft.FileShares resources. A 403 can indicate missing backup-semantics permission or account/network policy even when token acquisition succeeds. The token is not refreshed automatically; reacquire it before expiry.

First obtain the token with Entra client credentials or an Azure VM managed identity.

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoTextCompare
    Handle hoConfig
    Boolean iSameText1
    Handle hoEndpoint
    String sResourcePath
    Handle hoRest
    Variant vConnection
    Handle hoConnection
    Handle hoResult
    Handle hoSavedToken
    Boolean iSameText2
    Handle hoExpiry
    Variant vNow
    Handle hoNow
    String sAccessToken
    Handle hoBearer
    Handle hoFiles
    Variant vSink
    Handle hoSink
    Integer iHttpStatus
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  StringBuilder compares text by value in every generated language.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoTextCompare
    If (Not(IsComObjectCreated(hoTextCompare))) Begin
        Send CreateComObject of hoTextCompare
    End
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Move False To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoConfig
    If (Not(IsComObjectCreated(hoConfig))) Begin
        Send CreateComObject of hoConfig
    End
    Set ComStrict Of hoConfig To True
    Get ComLoadFile Of hoConfig "qa_data/azure/storage.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load private Azure configuration."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComStringOf Of hoConfig "account" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText1
    Get ComJsonTypeOf Of hoConfig "account" To iTemp1
    If ((iTemp1 <> 1) Or (iSameText1 = True)) Begin
        Showln "Supply the storage account name."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
    If (Not(IsComObjectCreated(hoEndpoint))) Begin
        Send CreateComObject of hoEndpoint
    End
    Get ComStringOf Of hoConfig "account" To sTemp1
    Get ComAppend Of hoEndpoint sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAppend Of hoEndpoint ".file.core.windows.net" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    //  resourcePath is already URL-encoded. Encode each path segment once when adapting it.
    //  Keep slash separators; names and paths are case-sensitive.
    Move "/examples/files/example.bin" To sResourcePath
    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End
    Set ComConnectTimeoutMs Of hoRest To 20000
    Set ComIdleTimeoutMs Of hoRest To 60000
    Get Create (RefClass(cComChilkatSocket)) To hoConnection
    If (Not(IsComObjectCreated(hoConnection))) Begin
        Send CreateComObject of hoConnection
    End
    Set ComRequireSslCertVerify Of hoConnection To True
    Set ComMaxReadIdleMs Of hoConnection To 60000
    Set ComMaxSendIdleMs Of hoConnection To 60000
    Get ComGetAsString Of hoEndpoint To sTemp1
    Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
    If (iSuccess <> True) Begin
        Showln "TLS connection failed; inspect diagnostics privately."
        Procedure_Return
    End

    //  Do not replay a write automatically after losing its response.
    Get pvComObject of hoConnection to vConnection
    Get ComUseConnection Of hoRest vConnection False To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatXml)) To hoResult
    If (Not(IsComObjectCreated(hoResult))) Begin
        Send CreateComObject of hoResult
    End
    Set ComStrict Of hoResult To True
    Get ComAddHeader Of hoRest "x-ms-version" "2026-04-06" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    //  Obtain this private file with either authentication example. Reacquire an expired token.
    Get Create (RefClass(cComChilkatJsonObject)) To hoSavedToken
    If (Not(IsComObjectCreated(hoSavedToken))) Begin
        Send CreateComObject of hoSavedToken
    End
    Set ComStrict Of hoSavedToken To True
    Get ComLoadFile Of hoSavedToken "qa_data/azure/storage-token.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load Storage token."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComStringOf Of hoSavedToken "access_token" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText2
    Get ComJsonTypeOf Of hoSavedToken "access_token" To iTemp1
    If ((iTemp1 <> 1) Or (iSameText2 = True)) Begin
        Showln "Missing access token."
        Procedure_Return
    End

    Get Create (RefClass(cComCkDateTime)) To hoExpiry
    If (Not(IsComObjectCreated(hoExpiry))) Begin
        Send CreateComObject of hoExpiry
    End
    Get Create (RefClass(cComCkDateTime)) To hoNow
    If (Not(IsComObjectCreated(hoNow))) Begin
        Send CreateComObject of hoNow
    End
    Get ComStringOf Of hoSavedToken "expires_at" To sTemp1
    Get ComSetFromTimestamp Of hoExpiry sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Invalid token expiry."
        Procedure_Return
    End

    Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComDiffSeconds Of hoExpiry     Get pvComObject of hoNow to vNow
vNow To iTemp1
    If (iTemp1 < 60) Begin
        Showln "Acquire a new Storage token before continuing."
        Procedure_Return
    End

    Get ComStringOf Of hoSavedToken "access_token" To sAccessToken
    Get Create (RefClass(cComChilkatStringBuilder)) To hoBearer
    If (Not(IsComObjectCreated(hoBearer))) Begin
        Send CreateComObject of hoBearer
    End
    Get ComAppend Of hoBearer "Bearer " To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAppend Of hoBearer sAccessToken To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetAsString Of hoBearer To sTemp1
    Get ComAddHeader Of hoRest "Authorization" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "x-ms-file-request-intent" "backup" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get Create (RefClass(cComCkFileAccess)) To hoFiles
    If (Not(IsComObjectCreated(hoFiles))) Begin
        Send CreateComObject of hoFiles
    End
    Get ComFileExists Of hoFiles "qa_output/azure-file.bin" To bTemp1
    If (bTemp1 = True) Begin
        Showln "Choose a new destination filename."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStream)) To hoSink
    If (Not(IsComObjectCreated(hoSink))) Begin
        Send CreateComObject of hoSink
    End
    Set ComSinkFile Of hoSink To "qa_output/azure-file.bin.part"
    Get ComSendReqNoBody Of hoRest "GET" sResourcePath To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComReadResponseHeader Of hoRest To iHttpStatus
    If (iHttpStatus < 0) Begin
        Showln "Could not read response headers."
        Procedure_Return
    End

    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComResponseStatusCode Of hoRest To iTemp1
        Showln "HTTP status: " iTemp1
        Get ComResponseHdrByName Of hoRest "x-ms-error-code" To sTemp1
        Showln "Azure error code: " sTemp1
        Get ComResponseHdrByName Of hoRest "x-ms-request-id" To sTemp1
        Showln "Request ID: " sTemp1
        //  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
        Procedure_Return
    End

    Get pvComObject of hoSink to vSink
    Get ComReadRespBodyStream Of hoRest vSink False To iSuccess
    If (iSuccess <> True) Begin
        Showln "Transfer incomplete; the .part file is not a completed download."
        Procedure_Return
    End

    Get ComFileRename Of hoFiles "qa_output/azure-file.bin.part" "qa_output/azure-file.bin" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not publish downloaded file."
        Procedure_Return
    End

    Showln "File downloaded using Entra authorization."


End_Procedure