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

Azure Storage: Create a Short-Lived Service SAS for One Blob

See more Azure Cloud Storage Examples

Create an HTTPS-only service SAS for writing exactly /examplecontainer/example.bin. The canonical resource is URL-decoded and includes /blob/account/container/blob. This is a Blob service SAS, not a universal layout for Files, Tables or Queues. It uses the modern signature layout including the encryption-scope field, even when empty. Prefer user delegation SAS when Entra-based delegation is available.

The token starts five minutes in the past for clock skew and expires fifteen minutes from now; account policy may require a shorter interval. Save it to a private token file and never log it. Generating a token locally does not validate the key, permissions, resource existence or account policy. Protect the signing account key and prefer short-lived delegation.

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 create service sas reference.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoTextCompare
    Handle hoConfig
    Boolean iSameText1
    Boolean iSameText2
    Handle hoSas
    Handle hoCanonical
    Handle hoClock
    String sToken
    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

    //  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(cComChilkatAuthAzureSAS)) To hoSas
    If (Not(IsComObjectCreated(hoSas))) Begin
        Send CreateComObject of hoSas
    End
    Get ComStringOf Of hoConfig "access_key" To sTemp1
    Set ComAccessKey Of hoSas To sTemp1
    //  Commas represent LF separators; omitted optional fields still occupy their positions.
    //  This layout includes signedEncryptionScope (ses), required since version 2020-12-06.
    Set ComStringToSign Of hoSas To "sp,st,se,resource,si,sip,spr,sv,sr,snapshot,ses,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 ComSetTokenParam Of hoSas "sr" "sr" "b" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComSetTokenParam Of hoSas "sp" "sp" "w" 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 Create (RefClass(cComCkDateTime)) To hoClock
    If (Not(IsComObjectCreated(hoClock))) Begin
        Send CreateComObject of hoClock
    End
    Get ComSetFromCurrentSystemTime Of hoClock To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

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

    Get ComGetAsTimestamp Of hoClock False 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 ComAddSeconds Of hoClock 1200 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

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

    //  Start is five minutes ago; expiry is fifteen minutes from now.
    //  Respect any stricter account SAS-expiration policy.
    Get ComGenerateToken Of hoSas To sToken
    Get ComLastMethodSuccess Of hoSas To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Could not generate SAS."
        Procedure_Return
    End

    Get Create (RefClass(cComCkFileAccess)) To hoFiles
    If (Not(IsComObjectCreated(hoFiles))) Begin
        Send CreateComObject of hoFiles
    End
    Get ComWriteEntireTextFile Of hoFiles "qa_data/azure/blob-sas.txt" sToken "utf-8" False To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not save token privately."
        Procedure_Return
    End

    Showln "SAS saved privately; do not print the token."


End_Procedure