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

Azure Storage: Create a Short-Lived Account SAS for Blob Writes

See more Azure Cloud Storage Examples

Create an HTTPS-only account SAS for Blob object writes (ss=b, srt=o, sp=w). It does not grant listing or deletion, but it still covers matching objects across the account; it is not constrained to one container or blob. Prefer a narrowly scoped service/user delegation SAS when that is the intended scope. Account SAS cannot refer to a stored access policy.

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 account sas reference.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  StringBuilder compares text by value in every generated language.
    DECLARE @textCompare int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @textCompare OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0
    DECLARE @config int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @config OUT

    EXEC sp_OASetProperty @config, 'Strict', 1
    EXEC sp_OAMethod @config, 'LoadFile', @success OUT, 'qa_data/azure/storage.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load private Azure configuration.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        RETURN
      END
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'account'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText1 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText1 OUT, '', 1
    EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'account'
    IF (@iTmp0 <> 1) or (@sameText1 = 1)
      BEGIN

        PRINT 'Supply the storage account name.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        RETURN
      END
    --  access_key is the base64 account key, not a connection string or SAS.
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText2 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText2 OUT, '', 1
    EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
    IF (@iTmp0 <> 1) or (@sameText2 = 1)
      BEGIN

        PRINT 'Supply the base64 account access key.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        RETURN
      END
    DECLARE @sas int
    EXEC @hr = sp_OACreate 'Chilkat.AuthAzureSAS', @sas OUT

    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OASetProperty @sas, 'AccessKey', @sTmp0
    --  Commas represent LF separators; omitted optional fields still occupy their positions.
    --  This layout includes signedEncryptionScope (ses), required since version 2020-12-06.
    EXEC sp_OASetProperty @sas, 'StringToSign', 'account,sp,ss,srt,st,se,sip,spr,sv,ses,'
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'account'
    EXEC sp_OAMethod @sas, 'SetNonTokenParam', @success OUT, 'account', @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        RETURN
      END
    EXEC sp_OAMethod @sas, 'SetTokenParam', @success OUT, 'ss', 'ss', 'b'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        RETURN
      END
    EXEC sp_OAMethod @sas, 'SetTokenParam', @success OUT, 'srt', 'srt', 'o'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        RETURN
      END
    EXEC sp_OAMethod @sas, 'SetTokenParam', @success OUT, 'sp', 'sp', 'w'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        RETURN
      END
    EXEC sp_OAMethod @sas, 'SetTokenParam', @success OUT, 'spr', 'spr', 'https'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        RETURN
      END
    EXEC sp_OAMethod @sas, 'SetTokenParam', @success OUT, 'sv', 'sv', '2026-04-06'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        RETURN
      END
    DECLARE @clock int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @clock OUT

    EXEC sp_OAMethod @clock, 'SetFromCurrentSystemTime', @success OUT
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        EXEC @hr = sp_OADestroy @clock
        RETURN
      END
    EXEC sp_OAMethod @clock, 'AddSeconds', @success OUT, -300
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        EXEC @hr = sp_OADestroy @clock
        RETURN
      END
    EXEC sp_OAMethod @clock, 'GetAsTimestamp', @sTmp0 OUT, 0
    EXEC sp_OAMethod @sas, 'SetTokenParam', @success OUT, 'st', 'st', @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        EXEC @hr = sp_OADestroy @clock
        RETURN
      END
    EXEC sp_OAMethod @clock, 'AddSeconds', @success OUT, 1200
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        EXEC @hr = sp_OADestroy @clock
        RETURN
      END
    EXEC sp_OAMethod @clock, 'GetAsTimestamp', @sTmp0 OUT, 0
    EXEC sp_OAMethod @sas, 'SetTokenParam', @success OUT, 'se', 'se', @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        EXEC @hr = sp_OADestroy @clock
        RETURN
      END
    --  Start is five minutes ago; expiry is fifteen minutes from now.
    --  Respect any stricter account SAS-expiration policy.
    DECLARE @token nvarchar(4000)
    EXEC sp_OAMethod @sas, 'GenerateToken', @token OUT
    EXEC sp_OAGetProperty @sas, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'Could not generate SAS.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        EXEC @hr = sp_OADestroy @clock
        RETURN
      END
    DECLARE @files int
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @files OUT

    EXEC sp_OAMethod @files, 'WriteEntireTextFile', @success OUT, 'qa_data/azure/account-sas.txt', @token, 'utf-8', 0
    IF @success <> 1
      BEGIN

        PRINT 'Could not save token privately.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @sas
        EXEC @hr = sp_OADestroy @clock
        EXEC @hr = sp_OADestroy @files
        RETURN
      END

    PRINT 'SAS saved privately; do not print the token.'

    EXEC @hr = sp_OADestroy @textCompare
    EXEC @hr = sp_OADestroy @config
    EXEC @hr = sp_OADestroy @sas
    EXEC @hr = sp_OADestroy @clock
    EXEC @hr = sp_OADestroy @files


END
GO