Sample code for 30+ languages & platforms
SQL Server

S3 List Buckets using an STS Session Token

See more AWS Security Token Service Examples

This is an example showing how to use an STS session token in an Amazon AWS request. This example will list S3 buckets using a previously obtained session token.

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
    -- 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.

    -- See Get AWS STS Session Token for sample code to get the session token XML.
    DECLARE @xmlToken int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xmlToken OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @xmlToken, 'LoadXmlFile', @success OUT, 'qa_data/tokens/aws_session_token.xml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @xmlToken, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmlToken
        RETURN
      END

    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT

    -- Connect to the Amazon AWS REST server.
    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 for the REST call.
    DECLARE @authAws int
    EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @authAws OUT

    -- The purpose of this example is to show how to use the temporary AccessKeyId, SecretAccessKey, and SessionToken.
    EXEC sp_OAMethod @xmlToken, 'GetChildContent', @sTmp0 OUT, 'GetSessionTokenResult|Credentials|AccessKeyId'
    EXEC sp_OASetProperty @authAws, 'AccessKey', @sTmp0
    EXEC sp_OAMethod @xmlToken, 'GetChildContent', @sTmp0 OUT, 'GetSessionTokenResult|Credentials|SecretAccessKey'
    EXEC sp_OASetProperty @authAws, 'SecretKey', @sTmp0
    EXEC sp_OASetProperty @authAws, 'ServiceName', 's3'
    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @authAws

    EXEC sp_OAMethod @xmlToken, 'GetChildContent', @sTmp0 OUT, 'GetSessionTokenResult|Credentials|SessionToken'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sTmp0

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

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/', @sbResponse
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmlToken
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @sbResponse
        RETURN
      END

    DECLARE @statusCode int
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @statusCode OUT

    PRINT 'Response status code = ' + @statusCode

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

    EXEC sp_OAMethod @xml, 'LoadSb', @success OUT, @sbResponse, 1
    EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

    IF @statusCode <> 200
      BEGIN

        PRINT 'Failed.  See error information in the XML.'
        EXEC @hr = sp_OADestroy @xmlToken
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @sbResponse
        EXEC @hr = sp_OADestroy @xml
        RETURN
      END

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


END
GO