SQL Server AWS S3: Get S3 Object Tags

Back to Index

Returns the tags associated with an object. This example gets the tags for the object named "starfish.jpg" located in the bucket "chilkat100".

Documentation: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGETtagging.html

CURL Command

curl -X GET https://chilkat100.s3.amazonaws.com/starfish.jpg?tagging

SQL Server Example

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @authAws int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.AuthAws', @authAws OUT

    EXEC sp_OASetProperty @authAws, 'AccessKey', 'AWS_ACCESS_KEY'
    EXEC sp_OASetProperty @authAws, 'SecretKey', 'AWS_SECRET_KEY'
    EXEC sp_OASetProperty @authAws, 'Region', 'us-east-1'
    EXEC sp_OASetProperty @authAws, 'ServiceName', 's3'
    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, STR(@authAws)

    --  URL: https://chilkat100.s3.amazonaws.com/starfish.jpg?tagging
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'chilkat100.s3.amazonaws.com', STR(@port), STR(@bTls), STR(@bAutoReconnect)
    IF STR(@success) <> 1
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ConnectFailReason', @iTmp0 OUT
        PRINT 'ConnectFailReason: ' + @iTmp0
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/starfish.jpg?tagging', STR(@sbResponseBody)
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END
    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @respStatusCode OUT
    IF STR(@respStatusCode) >= 400
      BEGIN

        PRINT 'Response Status Code = ' + STR(@respStatusCode)

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Response Body:'
        EXEC sp_OAMethod @sbResponseBody, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @xmlResponse int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Xml', @xmlResponse OUT

    EXEC sp_OAMethod @xmlResponse, 'LoadSb', @success OUT, STR(@sbResponseBody), 1

    --  See the Online Tool for Generating XML Parse Code
    DECLARE @Tagging_xmlns nvarchar(4000)

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @tagPath nvarchar(4000)

    DECLARE @Key nvarchar(4000)

    DECLARE @Value nvarchar(4000)

    EXEC sp_OAMethod @xmlResponse, 'GetAttrValue', @Tagging_xmlns OUT, 'xmlns'
    SELECT @i = 0
    EXEC sp_OAMethod @xmlResponse, 'NumChildrenHavingTag', @count_i OUT, 'TagSet|Tag'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @xmlResponse, 'I', STR(@i)
        EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @Key OUT, 'TagSet|Tag[i]|Key'
        EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @Value OUT, 'TagSet|Tag[i]|Value'
        SELECT @i = STR(@i) + 1
      END

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @authAws
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @xmlResponse


END
GO

Sample XML Response Body

<?xml version="1.0" encoding="UTF-8" ?>
<Tagging xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <TagSet>
        <Tag>
            <Key>habitat</Key>
            <Value>ocean</Value>
        </Tag>
        <Tag>
            <Key>phylum</Key>
            <Value>Echinodermata</Value>
        </Tag>
        <Tag>
            <Key>animal</Key>
            <Value>starfish</Value>
        </Tag>
    </TagSet>
</Tagging>