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 SQL Server Downloads
-- 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', 'sp,st,se,resource,si,sip,spr,sv,sr,snapshot,ses,rscc,rscd,rsce,rscl,rsct'
DECLARE @canonical int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @canonical OUT
EXEC sp_OAMethod @canonical, 'Append', @success OUT, '/blob/'
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 @canonical
RETURN
END
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'account'
EXEC sp_OAMethod @canonical, 'Append', @success OUT, @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 @canonical
RETURN
END
EXEC sp_OAMethod @canonical, 'Append', @success OUT, '/examplecontainer/example.bin'
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 @canonical
RETURN
END
EXEC sp_OAMethod @canonical, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @sas, 'SetNonTokenParam', @success OUT, 'resource', @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 @canonical
RETURN
END
EXEC sp_OAMethod @sas, 'SetTokenParam', @success OUT, 'sr', 'sr', '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
EXEC @hr = sp_OADestroy @canonical
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
EXEC @hr = sp_OADestroy @canonical
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
EXEC @hr = sp_OADestroy @canonical
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
EXEC @hr = sp_OADestroy @canonical
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 @canonical
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 @canonical
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 @canonical
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 @canonical
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 @canonical
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 @canonical
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/blob-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 @canonical
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 @canonical
EXEC @hr = sp_OADestroy @clock
EXEC @hr = sp_OADestroy @files
END
GO