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

Azure Storage: Authenticate with an Azure VM Managed Identity

See more Azure Cloud Storage Examples

Run on an Azure VM with a system-assigned or user-assigned managed identity. In storage.json supply account and optionally managed_client_id. Assign Storage Blob Data Reader (or a suitable data role) at the needed scope. The example obtains a Storage-audience token from IMDS and uses it for a Blob HEAD request. Do not send IMDS traffic through a proxy or make its destination user-controlled.

The token is saved to private qa_data/azure/storage-token.json with an expires_at timestamp for the related examples. Cache it until shortly before expiration, then repeat token acquisition. This flow does not use a refresh token. A 401 and a 403 need different investigation: token validity/audience versus data permissions or account policy. Allow time for RBAC changes to propagate.

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 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
    DECLARE @identity int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @identity OUT

    EXEC sp_OASetProperty @identity, 'ConnectTimeoutMs', 20000
    EXEC sp_OASetProperty @identity, 'IdleTimeoutMs', 60000
    DECLARE @expires int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @expires OUT

    EXEC sp_OAMethod @expires, '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 @identity
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    --  Azure VM IMDS only: fixed link-local endpoint, direct connection, no proxy.
    --  App Service, Functions and Azure Arc have different identity endpoints.
    EXEC sp_OAMethod @identity, 'Connect', @success OUT, '169.254.169.254', 80, 0, 0
    IF @success <> 1
      BEGIN

        PRINT 'IMDS is available only on an eligible Azure VM.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    EXEC sp_OAMethod @identity, 'AddHeader', @success OUT, 'Metadata', 'true'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    EXEC sp_OAMethod @identity, 'AddQueryParam', @success OUT, 'api-version', '2018-02-01'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    EXEC sp_OAMethod @identity, 'AddQueryParam', @success OUT, 'resource', 'https://storage.azure.com/'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    --  Optional user-assigned identity. Omit for the VM's system-assigned identity.
    EXEC sp_OAMethod @config, 'HasMember', @iTmp0 OUT, 'managed_client_id'
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @textCompare, 'Clear', NULL
        EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'managed_client_id'
        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, 'managed_client_id'
        IF (@iTmp0 <> 1) or (@sameText2 = 1)
          BEGIN

            PRINT 'Invalid managed_client_id.'
            EXEC @hr = sp_OADestroy @textCompare
            EXEC @hr = sp_OADestroy @config
            EXEC @hr = sp_OADestroy @identity
            EXEC @hr = sp_OADestroy @expires
            RETURN
          END
        EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'managed_client_id'
        EXEC sp_OAMethod @identity, 'AddQueryParam', @success OUT, 'client_id', @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 @identity
            EXEC @hr = sp_OADestroy @expires
            RETURN
          END
      END
    DECLARE @tokenText nvarchar(4000)
    EXEC sp_OAMethod @identity, 'FullRequestNoBody', @tokenText OUT, 'GET', '/metadata/identity/oauth2/token'
    EXEC sp_OAGetProperty @identity, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'Token request did not complete; inspect diagnostics privately.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    EXEC sp_OAGetProperty @identity, 'ResponseStatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        PRINT 'Token request failed; inspect the status privately, never print the response token.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    DECLARE @token int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT

    EXEC sp_OASetProperty @token, 'Strict', 1
    EXEC sp_OAMethod @token, 'Load', @success OUT, @tokenText
    IF @success <> 1
      BEGIN

        PRINT 'Expected token JSON.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText3 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText3 OUT, '', 1
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'token_type'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText4 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText4 OUT, 'Bearer', 1
    EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
    IF (@iTmp0 <> 1) or (@sameText3 = 1) or (@sameText4 <> 1)
      BEGIN

        PRINT 'Missing bearer token.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    DECLARE @seconds int
    EXEC sp_OAMethod @token, 'IntOf', @seconds OUT, 'expires_in'
    IF (@seconds < 120) or (@seconds > 86400)
      BEGIN

        PRINT 'Unexpected token lifetime.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @expires, 'AddSeconds', @success OUT, @seconds
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @expires, 'GetAsTimestamp', @sTmp0 OUT, 0
    EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'expires_at', @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 @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @token, 'WriteFile', @success OUT, 'qa_data/azure/storage-token.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not persist token privately.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    DECLARE @accessToken nvarchar(4000)
    EXEC sp_OAMethod @token, 'StringOf', @accessToken OUT, 'access_token'
    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 @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        RETURN
      END
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.blob.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 @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        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 = '/examplecontainer/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 @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        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 @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    DECLARE @responseText nvarchar(4000)

    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 @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    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 @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        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 @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        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 @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @bearer
        RETURN
      END
    EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'HEAD', @resourcePath
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'Request did not complete; outcome may be uncertain.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @bearer
        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 @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @bearer
        RETURN
      END

    PRINT 'Storage token obtained and blob properties read.'

    EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'ETag'
    PRINT 'ETag: ' + @sTmp0

    EXEC @hr = sp_OADestroy @textCompare
    EXEC @hr = sp_OADestroy @config
    EXEC @hr = sp_OADestroy @identity
    EXEC @hr = sp_OADestroy @expires
    EXEC @hr = sp_OADestroy @token
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @connection
    EXEC @hr = sp_OADestroy @result
    EXEC @hr = sp_OADestroy @bearer


END
GO