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

Azure Files: Create and Upload a Small File

See more Azure Cloud Storage Examples

Create or replace a file in an existing SMB share/directory, then write one range of up to 4 MiB. The inclusive range end is start + byte count - 1. Empty input creates a zero-length file without issuing an invalid range. Every local read and HTTP result is checked.

Replacement is disabled by default. Create File can truncate an existing file. Range writes are not an atomic whole-file upload; failure leaves a partially written target. Coordinate writers and use a temporary remote name plus a later publication workflow where appropriate. The sample deliberately bounds int offsets to 2,000,000,000 bytes and rejects larger files before creation; this is not Azure's file-size limit. API versions 2026-02-06 and later additionally support up to 4 MiB in Create File itself; the separate create/range pattern remains supported.

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 put range reference.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoTextCompare
    Boolean iApplyChange
    Handle hoConfig
    Boolean iSameText1
    Boolean iSameText2
    Handle hoEndpoint
    String sResourcePath
    Handle hoRest
    Variant vAzAuth
    Handle hoAzAuth
    Variant vConnection
    Handle hoConnection
    String sResponseText
    Variant vResponseBody
    Handle hoResponseBody
    Handle hoResult
    Handle hoInput
    String sFileSize
    Handle hoSizeText
    Handle hoSizeValue
    Integer iTotal
    Variant vChunk
    Handle hoChunk
    Handle hoRange
    Integer iOffset
    Integer iCount
    String sTemp1
    Integer iTemp1
    Boolean bTemp1
    Boolean bTemp2

    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
    Move False To iSuccess
    Move False To iApplyChange
    If (iApplyChange <> True) Begin
        Showln "File creation/replacement disabled. Review the target and set applyChange to true deliberately."
        Procedure_Return
    End

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    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

    //  access_key is the base64 account key, not a connection string or SAS.
    Send ComClear To hoTextCompare
    Get ComStringOf Of hoConfig "access_key" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText2
    Get ComJsonTypeOf Of hoConfig "access_key" To iTemp1
    If ((iTemp1 <> 1) Or (iSameText2 = True)) Begin
        Showln "Supply the base64 account access key."
        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(cComChilkatAuthAzureStorage)) To hoAzAuth
    If (Not(IsComObjectCreated(hoAzAuth))) Begin
        Send CreateComObject of hoAzAuth
    End
    Get ComStringOf Of hoConfig "account" To sTemp1
    Set ComAccount Of hoAzAuth To sTemp1
    Get ComStringOf Of hoConfig "access_key" To sTemp1
    Set ComAccessKey Of hoAzAuth To sTemp1
    Set ComService Of hoAzAuth To "File"
    Set ComScheme Of hoAzAuth To "SharedKey"
    Set ComXMsVersion Of hoAzAuth To "2026-04-06"
    Get pvComObject of hoAzAuth to vAzAuth
    Get ComSetAuthAzureStorage Of hoRest vAzAuth To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not configure Shared Key signing."
        Procedure_Return
    End

    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(cComChilkatStringBuilder)) To hoResponseBody
    If (Not(IsComObjectCreated(hoResponseBody))) Begin
        Send CreateComObject of hoResponseBody
    End
    Get Create (RefClass(cComChilkatXml)) To hoResult
    If (Not(IsComObjectCreated(hoResult))) Begin
        Send CreateComObject of hoResult
    End
    Set ComStrict Of hoResult To True
    Get Create (RefClass(cComCkFileAccess)) To hoInput
    If (Not(IsComObjectCreated(hoInput))) Begin
        Send CreateComObject of hoInput
    End
    Get ComFileSizeStr Of hoInput "qa_data/bin/sample.bin" To sFileSize
    Get ComLastMethodSuccess Of hoInput To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Could not get input size."
        Procedure_Return
    End

    //  This demonstration uses int offsets: reject sizes outside its explicit 2,000,000,000-byte bound.
    //  The bound is local sample arithmetic, not the Azure Files service size limit.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSizeText
    If (Not(IsComObjectCreated(hoSizeText))) Begin
        Send CreateComObject of hoSizeText
    End
    Get ComAppend Of hoSizeText sFileSize To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComLength Of hoSizeText To iTemp1
    If (iTemp1 > 10) Begin
        Showln "Input exceeds the demonstration size bound."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoSizeValue
    If (Not(IsComObjectCreated(hoSizeValue))) Begin
        Send CreateComObject of hoSizeValue
    End
    Get ComUpdateNumber Of hoSizeValue "size" sFileSize To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    //  Compare the decimal text before converting to a 32-bit int.
    Get ComLength Of hoSizeText To iTemp1
    Get ComStartsWith Of hoSizeText "1" True To bTemp1
    Get ComContentsEqual Of hoSizeText "2000000000" True To bTemp2
    If ((iTemp1 = 10) And (bTemp1 <> True) And (bTemp2 <> True)) Begin
        Showln "Input exceeds the demonstration size bound."
        Procedure_Return
    End

    Get ComIntOf Of hoSizeValue "size" To iTotal
    If (iTotal > 4194304) Begin
        Showln "Use the multi-range example for inputs larger than 4 MiB."
        Procedure_Return
    End

    Get ComOpenForRead Of hoInput "qa_data/bin/sample.bin" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not open input."
        Send ComFileClose To hoInput
        Procedure_Return
    End

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

    Get ComAddHeader Of hoRest "x-ms-content-length" sFileSize To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Send ComFileClose To hoInput
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "x-ms-file-permission" "inherit" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Send ComFileClose To hoInput
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "x-ms-file-attributes" "None" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Send ComFileClose To hoInput
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "x-ms-file-creation-time" "now" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Send ComFileClose To hoInput
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "x-ms-file-last-write-time" "now" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Send ComFileClose To hoInput
        Procedure_Return
    End

    Get ComFullRequestNoBody Of hoRest "PUT" sResourcePath To sResponseText
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Request did not complete; outcome may be uncertain."
        Send ComFileClose To hoInput
        Procedure_Return
    End

    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 201) 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.
        Send ComFileClose To hoInput
        Procedure_Return
    End

    //  Creation/replacement reserves the final length; subsequent ranges are separate operations.
    Get ComClearAllHeaders Of hoRest To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Send ComFileClose To hoInput
        Procedure_Return
    End

    Get ComAddQueryParam Of hoRest "comp" "range" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Send ComFileClose To hoInput
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "x-ms-write" "update" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Send ComFileClose To hoInput
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoChunk
    If (Not(IsComObjectCreated(hoChunk))) Begin
        Send CreateComObject of hoChunk
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoRange
    If (Not(IsComObjectCreated(hoRange))) Begin
        Send CreateComObject of hoRange
    End
    Move 0 To iOffset
    While (iOffset < iTotal)
        Move (iTotal - iOffset) To iCount
        If (iCount > 4194304) Begin
            Move 4194304 To iCount
        End

        Get ComClear Of hoChunk To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Get pvComObject of hoChunk to vChunk
        Get ComFileReadBd Of hoInput iCount vChunk To iSuccess
        If (iSuccess <> True) Begin
            Showln "Input read failed."
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Get ComNumBytes Of hoChunk To iTemp1
        If (iTemp1 <> iCount) Begin
            Showln "Input changed or a short read occurred."
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Send ComClear To hoRange
        Get ComAppend Of hoRange "bytes=" To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Get ComAppendInt Of hoRange iOffset To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Get ComAppend Of hoRange "-" To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Get ComAppendInt Of hoRange (iOffset + iCount - 1) To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Get ComGetAsString Of hoRange To sTemp1
        Get ComAddHeader Of hoRest "x-ms-range" sTemp1 To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Get ComDisconnect Of hoRest 10 To iSuccess
        Get ComGetAsString Of hoEndpoint To sTemp1
        Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
        If (iSuccess <> True) Begin
            Showln "TLS reconnection failed."
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Get pvComObject of hoConnection to vConnection
        Get ComUseConnection Of hoRest vConnection False To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Send ComClear To hoResponseBody
        Get pvComObject of hoChunk to vChunk
        Get pvComObject of hoResponseBody to vResponseBody
        Get ComFullRequestBd Of hoRest "PUT" sResourcePath vChunk vResponseBody To iSuccess
        If (iSuccess <> True) Begin
            Showln "Request did not complete; outcome may be uncertain."
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Get ComResponseStatusCode Of hoRest To iTemp1
        If (iTemp1 <> 201) 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.
            Send ComFileClose To hoInput
            Procedure_Return
        End

        Move (iOffset + iCount) To iOffset
    Loop

    Send ComFileClose To hoInput
    Showln "File content uploaded."


End_Procedure