Sample code for 30+ languages & platforms
SQL Server

S3 Add Tags to an Object

See more Amazon S3 (new) Examples

Demonstrates how to add one or more tags to an S3 object.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Connect to the Amazon AWS REST server in the desired region.
    -- (for us-east-1, we use "s3.amazonaws.com", but for another region, such as us-west-2, we would use "s3-us-west-2.amazonaws.com")
    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, 's3.amazonaws.com', @port, @bTls, @bAutoReconnect

    -- Provide AWS credentials.
    DECLARE @authAws int
    EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @authAws OUT

    EXEC sp_OASetProperty @authAws, 'AccessKey', 'AWS_ACCESS_KEY'
    EXEC sp_OASetProperty @authAws, 'SecretKey', 'AWS_SECRET_KEY'
    EXEC sp_OASetProperty @authAws, 'ServiceName', 's3'
    EXEC sp_OASetProperty @authAws, 'Region', 'us-east-1'

    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @authAws

    -- Set the bucket name via the HOST header.
    -- In this case, the bucket name is "chilkat100".
    -- Note that the Host header should use "bucketName.s3.amazonaws.com", not "bucketName.s3-us-east-1.amazonaws.com"
    -- The same applies to aother regions.  The Host header should simply be <bucketName>.s3.amazonaws.com regardless of the region.
    EXEC sp_OASetProperty @rest, 'Host', 'chilkat100.s3.amazonaws.com'

    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT

    EXEC sp_OASetProperty @xml, 'Tag', 'Tagging'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'TagSet|Tag|Key', 'plant'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'TagSet|Tag|Value', 'chili pepper'

    DECLARE @sbRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRequestBody OUT

    EXEC sp_OAMethod @xml, 'GetXmlSb', @success OUT, @sbRequestBody

    -- It is important to add the terminating "=" after the "?tagging".
    DECLARE @sbResponse int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponse OUT

    EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'PUT', '/chiliPepper.gif?tagging=', @sbRequestBody, @sbResponse
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @xml
        EXEC @hr = sp_OADestroy @sbRequestBody
        EXEC @hr = sp_OADestroy @sbResponse
        RETURN
      END


    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    PRINT 'Response status code: ' + @iTmp0

    -- When successful, the S3 Storage service will respond with a 200 response code,
    -- with an XML body.  
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN
        -- Examine the request/response to see what happened.

        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        PRINT 'response status code = ' + @iTmp0

        EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT
        PRINT 'response status text = ' + @sTmp0

        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT 'response header: ' + @sTmp0

        EXEC sp_OAMethod @sbResponse, 'GetAsString', @sTmp0 OUT
        PRINT 'response body: ' + @sTmp0

        PRINT '---'

        EXEC sp_OAGetProperty @rest, 'LastRequestStartLine', @sTmp0 OUT
        PRINT 'LastRequestStartLine: ' + @sTmp0

        EXEC sp_OAGetProperty @rest, 'LastRequestHeader', @sTmp0 OUT
        PRINT 'LastRequestHeader: ' + @sTmp0
      END

    EXEC sp_OAMethod @sbResponse, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @authAws
    EXEC @hr = sp_OADestroy @xml
    EXEC @hr = sp_OADestroy @sbRequestBody
    EXEC @hr = sp_OADestroy @sbResponse


END
GO