Azure Files: Download with Entra OAuth and Backup Intent
See more Azure Cloud Storage Examples
Use a Storage-audience token saved by either authentication example, then stream a file through FileREST with x-ms-file-request-intent: backup. The identity needs file read permission and readFileBackupSemantics; Storage File Data Privileged Reader is the built-in read role for this use. This privileged access can bypass file/directory ACLs within the role's scope. Ordinary SMB Share Reader permissions alone are insufficient.
This is REST authorization, not SMB/Kerberos authentication. It applies to classic Azure file shares created through Microsoft.Storage, not Microsoft.FileShares resources. A 403 can indicate missing backup-semantics permission or account/network policy even when token acquisition succeeds. The token is not refreshed automatically; reacquire it before expiry.
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.
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
DECLARE @endpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'account'
EXEC sp_OAMethod @endpoint, '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 @endpoint
RETURN
END
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.file.core.windows.net'
IF @success <> 1
BEGIN
PRINT 'Could not prepare the request.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
RETURN
END
-- resourcePath is already URL-encoded. Encode each path segment once when adapting it.
-- Keep slash separators; names and paths are case-sensitive.
DECLARE @resourcePath nvarchar(4000)
SELECT @resourcePath = '/examples/files/example.bin'
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
EXEC sp_OASetProperty @rest, 'ConnectTimeoutMs', 20000
EXEC sp_OASetProperty @rest, 'IdleTimeoutMs', 60000
DECLARE @connection int
EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT
EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @connection, 'MaxReadIdleMs', 60000
EXEC sp_OASetProperty @connection, 'MaxSendIdleMs', 60000
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
IF @success <> 1
BEGIN
PRINT 'TLS connection failed; inspect diagnostics privately.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
-- Do not replay a write automatically after losing its response.
EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 0
IF @success <> 1
BEGIN
PRINT 'Could not prepare the request.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.Xml', @result OUT
EXEC sp_OASetProperty @result, 'Strict', 1
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'x-ms-version', '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 @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
RETURN
END
-- Obtain this private file with either authentication example. Reacquire an expired token.
DECLARE @savedToken int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @savedToken OUT
EXEC sp_OASetProperty @savedToken, 'Strict', 1
EXEC sp_OAMethod @savedToken, 'LoadFile', @success OUT, 'qa_data/azure/storage-token.json'
IF @success <> 1
BEGIN
PRINT 'Could not load Storage token.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
RETURN
END
EXEC sp_OAMethod @textCompare, 'Clear', NULL
EXEC sp_OAMethod @savedToken, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
DECLARE @sameText2 int
EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText2 OUT, '', 1
EXEC sp_OAMethod @savedToken, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
IF (@iTmp0 <> 1) or (@sameText2 = 1)
BEGIN
PRINT 'Missing access token.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
RETURN
END
DECLARE @expiry int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @expiry OUT
DECLARE @now int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @now OUT
EXEC sp_OAMethod @savedToken, 'StringOf', @sTmp0 OUT, 'expires_at'
EXEC sp_OAMethod @expiry, 'SetFromTimestamp', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Invalid token expiry.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
RETURN
END
EXEC sp_OAMethod @now, '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 @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
RETURN
END
EXEC sp_OAMethod @expiry, 'DiffSeconds', @iTmp0 OUT, @now
IF @iTmp0 < 60
BEGIN
PRINT 'Acquire a new Storage token before continuing.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
RETURN
END
DECLARE @accessToken nvarchar(4000)
EXEC sp_OAMethod @savedToken, 'StringOf', @accessToken OUT, 'access_token'
DECLARE @bearer int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @bearer OUT
EXEC sp_OAMethod @bearer, 'Append', @success OUT, 'Bearer '
IF @success <> 1
BEGIN
PRINT 'Could not prepare the request.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @bearer
RETURN
END
EXEC sp_OAMethod @bearer, 'Append', @success OUT, @accessToken
IF @success <> 1
BEGIN
PRINT 'Could not prepare the request.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @bearer
RETURN
END
EXEC sp_OAMethod @bearer, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Authorization', @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 @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @bearer
RETURN
END
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'x-ms-file-request-intent', 'backup'
IF @success <> 1
BEGIN
PRINT 'Could not prepare the request.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @bearer
RETURN
END
DECLARE @files int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @files OUT
EXEC sp_OAMethod @files, 'FileExists', @iTmp0 OUT, 'qa_output/azure-file.bin'
IF @iTmp0 = 1
BEGIN
PRINT 'Choose a new destination filename.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @bearer
EXEC @hr = sp_OADestroy @files
RETURN
END
DECLARE @sink int
EXEC @hr = sp_OACreate 'Chilkat.Stream', @sink OUT
EXEC sp_OASetProperty @sink, 'SinkFile', 'qa_output/azure-file.bin.part'
EXEC sp_OAMethod @rest, 'SendReqNoBody', @success OUT, 'GET', @resourcePath
IF @success <> 1
BEGIN
PRINT 'Could not prepare the request.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @bearer
EXEC @hr = sp_OADestroy @files
EXEC @hr = sp_OADestroy @sink
RETURN
END
DECLARE @httpStatus int
EXEC sp_OAMethod @rest, 'ReadResponseHeader', @httpStatus OUT
IF @httpStatus < 0
BEGIN
PRINT 'Could not read response headers.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @bearer
EXEC @hr = sp_OADestroy @files
EXEC @hr = sp_OADestroy @sink
RETURN
END
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
PRINT 'HTTP status: ' + @iTmp0
EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-error-code'
PRINT 'Azure error code: ' + @sTmp0
EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-request-id'
PRINT 'Request ID: ' + @sTmp0
-- Inspect any error body privately; do not log credentials, SAS URLs or payloads.
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @bearer
EXEC @hr = sp_OADestroy @files
EXEC @hr = sp_OADestroy @sink
RETURN
END
EXEC sp_OAMethod @rest, 'ReadRespBodyStream', @success OUT, @sink, 0
IF @success <> 1
BEGIN
PRINT 'Transfer incomplete; the .part file is not a completed download.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @bearer
EXEC @hr = sp_OADestroy @files
EXEC @hr = sp_OADestroy @sink
RETURN
END
EXEC sp_OAMethod @files, 'FileRename', @success OUT, 'qa_output/azure-file.bin.part', 'qa_output/azure-file.bin'
IF @success <> 1
BEGIN
PRINT 'Could not publish downloaded file.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @bearer
EXEC @hr = sp_OADestroy @files
EXEC @hr = sp_OADestroy @sink
RETURN
END
PRINT 'File downloaded using Entra authorization.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @savedToken
EXEC @hr = sp_OADestroy @expiry
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @bearer
EXEC @hr = sp_OADestroy @files
EXEC @hr = sp_OADestroy @sink
END
GO