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

Azure Blob: Read Properties and Conditionally Update Metadata

See more Azure Cloud Storage Examples

Use HEAD to read size, content type and ETag without downloading content. Only 404 is treated as not found; 403 is an authorization failure, not proof of absence. Keep the ETag exactly as returned, including its quotes.

Set updateMetadata to true to replace the blob's metadata with the demonstrated key/value pairs. If-Match prevents the update if the blob changed after HEAD. A 412 requires rereading and deciding whether to merge or retry; do not blindly overwrite. Metadata and blob index tags are different features. The example replaces the full metadata collection; preserve existing entries explicitly when needed.

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
    --  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 @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, '.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 @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 @azAuth int
    EXEC @hr = sp_OACreate 'Chilkat.AuthAzureStorage', @azAuth OUT

    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'account'
    EXEC sp_OASetProperty @azAuth, 'Account', @sTmp0
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OASetProperty @azAuth, 'AccessKey', @sTmp0
    EXEC sp_OASetProperty @azAuth, 'Service', 'Blob'
    EXEC sp_OASetProperty @azAuth, 'Scheme', 'SharedKey'
    EXEC sp_OASetProperty @azAuth, 'XMsVersion', '2026-04-06'
    EXEC sp_OAMethod @rest, 'SetAuthAzureStorage', @success OUT, @azAuth
    IF @success <> 1
      BEGIN

        PRINT 'Could not configure Shared Key signing.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        RETURN
      END
    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 @azAuth
        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 @azAuth
        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
    --  Leave false to inspect only. Setting metadata replaces the full metadata collection.
    DECLARE @updateMetadata int
    SELECT @updateMetadata = 0
    EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'HEAD', @resourcePath
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'HEAD did not complete.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    IF @iTmp0 = 404
      BEGIN

        PRINT 'Blob not found (or its container is absent).'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        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 @azAuth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    DECLARE @etag nvarchar(4000)
    EXEC sp_OAMethod @rest, 'ResponseHdrByName', @etag OUT, 'ETag'
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @etag
    DECLARE @sameText3 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText3 OUT, '', 1
    IF @sameText3 = 1
      BEGIN

        PRINT 'Missing ETag.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        RETURN
      END

    PRINT 'ETag: ' + @etag

    EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'Content-Length'
    PRINT 'Size: ' + @sTmp0

    EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'Content-Type'
    PRINT 'Content type: ' + @sTmp0
    IF @updateMetadata = 1
      BEGIN
        EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'comp', 'metadata'
        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 @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            RETURN
          END
        EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'x-ms-meta-category', 'example'
        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 @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            RETURN
          END
        EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'x-ms-meta-format', 'binary'
        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 @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            RETURN
          END
        EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'If-Match', @etag
        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 @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            RETURN
          END
        EXEC sp_OAMethod @rest, 'Disconnect', @success OUT, 10
        EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
        EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
        IF @success <> 1
          BEGIN

            PRINT 'TLS reconnection failed.'
            EXEC @hr = sp_OADestroy @textCompare
            EXEC @hr = sp_OADestroy @config
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            RETURN
          END
        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 @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            RETURN
          END
        EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'PUT', @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 @endpoint
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            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 @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            RETURN
          END

        PRINT 'Metadata replaced. A stale ETag would produce 412 without changing metadata.'
      END

    PRINT 'Blob properties inspected.'

    EXEC @hr = sp_OADestroy @textCompare
    EXEC @hr = sp_OADestroy @config
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @azAuth
    EXEC @hr = sp_OADestroy @connection
    EXEC @hr = sp_OADestroy @result


END
GO