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

Azure Blob: Create a Read-Only User Delegation SAS

See more Azure Cloud Storage Examples

Use the Storage token saved by either authentication example to obtain a user delegation key and create an HTTPS-only read SAS for examplecontainer/example.bin. The principal needs generateUserDelegationKey permission at storage-account scope (or above), plus read access to the blob; Storage Blob Delegator alone does not grant content access. SAS permissions cannot exceed the principal's effective permissions.

Uses the returned key validity timestamps, with a requested start five minutes ago and expiry fifteen minutes ahead. User delegation keys cannot exceed seven days. Respect stricter account SAS-expiration policies. Save the complete URL privately; anyone holding this ordinary bearer SAS can use it until expiry. This example does not enable optional user-bound SAS, signed headers/query restrictions, or stored access policies (unsupported for user delegation SAS).

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
    String sResponseText
    Handle hoResult
    Handle hoSavedToken
    Boolean iSameText2
    Handle hoExpiry
    Variant vNow
    Handle hoNow
    String sAccessToken
    Handle hoBearer
    Handle hoWindow
    String sSignedStart
    String sSignedExpiry
    Handle hoKeyRequest
    Boolean iSameText3
    Boolean iSameText4
    Boolean iSameText5
    Boolean iSameText6
    Boolean iSameText7
    Boolean iSameText8
    Boolean iSameText9
    Boolean iSameText10
    Handle hoSas
    Handle hoCanonical
    String sSasToken
    Handle hoSasUrl
    Handle hoFiles
    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 ".blob.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 "/" 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 ComAddQueryParam Of hoRest "restype" "service" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

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

    Get ComAddHeader Of hoRest "Content-Type" "application/xml" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get Create (RefClass(cComCkDateTime)) To hoWindow
    If (Not(IsComObjectCreated(hoWindow))) Begin
        Send CreateComObject of hoWindow
    End
    Get ComSetFromCurrentSystemTime Of hoWindow To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAddSeconds Of hoWindow -300 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetAsTimestamp Of hoWindow False To sSignedStart
    Get ComAddSeconds Of hoWindow 1200 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetAsTimestamp Of hoWindow False To sSignedExpiry
    Get Create (RefClass(cComChilkatXml)) To hoKeyRequest
    If (Not(IsComObjectCreated(hoKeyRequest))) Begin
        Send CreateComObject of hoKeyRequest
    End
    Set ComTag Of hoKeyRequest To "KeyInfo"
    Send ComNewChild2 To hoKeyRequest "Start" sSignedStart
    Send ComNewChild2 To hoKeyRequest "Expiry" sSignedExpiry
    Get ComGetXml Of hoKeyRequest To sTemp1
    Get ComFullRequestString Of hoRest "POST" sResourcePath sTemp1 To sResponseText
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Request did not complete; outcome may be uncertain."
        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 ComLoadXml Of hoResult sResponseText To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected well-formed XML."
        Procedure_Return
    End

    Get ComTagEquals Of hoResult "UserDelegationKey" To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Unexpected XML response root."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComGetChildContent Of hoResult "SignedOid" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText3
    If (iSameText3 = True) Begin
        Showln "Incomplete delegation-key response."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComGetChildContent Of hoResult "SignedTid" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText4
    If (iSameText4 = True) Begin
        Showln "Incomplete delegation-key response."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComGetChildContent Of hoResult "SignedStart" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText5
    If (iSameText5 = True) Begin
        Showln "Incomplete delegation-key response."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComGetChildContent Of hoResult "SignedExpiry" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText6
    If (iSameText6 = True) Begin
        Showln "Incomplete delegation-key response."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComGetChildContent Of hoResult "SignedService" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText7
    If (iSameText7 = True) Begin
        Showln "Incomplete delegation-key response."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComGetChildContent Of hoResult "SignedVersion" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText8
    If (iSameText8 = True) Begin
        Showln "Incomplete delegation-key response."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComGetChildContent Of hoResult "Value" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText9
    If (iSameText9 = True) Begin
        Showln "Incomplete delegation-key response."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComGetChildContent Of hoResult "SignedService" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "b" True To iSameText10
    If (iSameText10 <> True) Begin
        Showln "Expected a Blob delegation key."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatAuthAzureSAS)) To hoSas
    If (Not(IsComObjectCreated(hoSas))) Begin
        Send CreateComObject of hoSas
    End
    //  The returned key Value is base64. It is not the storage account key.
    Get ComGetChildContent Of hoResult "Value" To sTemp1
    Set ComAccessKey Of hoSas To sTemp1
    //  2026-04-06 layout includes the optional user binding and signed request fields.
    //  They remain empty here, but their LF-separated positions must still exist.
    Set ComStringToSign Of hoSas To "sp,st,se,resource,skoid,sktid,skt,ske,sks,skv,saoid,suoid,scid,skdutid,sduoid,sip,spr,sv,sr,snapshot,ses,headers,query,rscc,rscd,rsce,rscl,rsct"
    Get Create (RefClass(cComChilkatStringBuilder)) To hoCanonical
    If (Not(IsComObjectCreated(hoCanonical))) Begin
        Send CreateComObject of hoCanonical
    End
    Get ComAppend Of hoCanonical "/blob/" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComStringOf Of hoConfig "account" To sTemp1
    Get ComAppend Of hoCanonical sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAppend Of hoCanonical "/examplecontainer/example.bin" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetAsString Of hoCanonical To sTemp1
    Get ComSetNonTokenParam Of hoSas "resource" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetChildContent Of hoResult "SignedOid" To sTemp1
    Get ComSetTokenParam Of hoSas "skoid" "skoid" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetChildContent Of hoResult "SignedTid" To sTemp1
    Get ComSetTokenParam Of hoSas "sktid" "sktid" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetChildContent Of hoResult "SignedStart" To sTemp1
    Get ComSetTokenParam Of hoSas "skt" "skt" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetChildContent Of hoResult "SignedExpiry" To sTemp1
    Get ComSetTokenParam Of hoSas "ske" "ske" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetChildContent Of hoResult "SignedService" To sTemp1
    Get ComSetTokenParam Of hoSas "sks" "sks" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetChildContent Of hoResult "SignedVersion" To sTemp1
    Get ComSetTokenParam Of hoSas "skv" "skv" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComSetTokenParam Of hoSas "sp" "sp" "r" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetChildContent Of hoResult "SignedStart" To sTemp1
    Get ComSetTokenParam Of hoSas "st" "st" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetChildContent Of hoResult "SignedExpiry" To sTemp1
    Get ComSetTokenParam Of hoSas "se" "se" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComSetTokenParam Of hoSas "spr" "spr" "https" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComSetTokenParam Of hoSas "sv" "sv" "2026-04-06" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComSetTokenParam Of hoSas "sr" "sr" "b" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGenerateToken Of hoSas To sSasToken
    Get ComLastMethodSuccess Of hoSas To bTemp1
    If (bTemp1 <> True) Begin
        Showln "SAS generation failed."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSasUrl
    If (Not(IsComObjectCreated(hoSasUrl))) Begin
        Send CreateComObject of hoSasUrl
    End
    Get ComAppend Of hoSasUrl "https://" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetAsString Of hoEndpoint To sTemp1
    Get ComAppend Of hoSasUrl sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAppend Of hoSasUrl "/examplecontainer/example.bin?" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAppend Of hoSasUrl sSasToken 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 ComGetAsString Of hoSasUrl To sTemp1
    Get ComWriteEntireTextFile Of hoFiles "qa_data/azure/read-blob-url.txt" sTemp1 "utf-8" False To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not save the SAS URL privately."
        Procedure_Return
    End

    Showln "Read-only blob SAS URL saved privately; it expires in approximately 15 minutes."


End_Procedure