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

S3: Download UTF-8 Text from a Regional Bucket

See more S3 - HTTP Examples

Uses Chilkat.Http with Amazon S3 general purpose buckets and Signature Version 4. These examples do not cover directory buckets/S3 Express, access-point ARNs, Outposts, or other specialized endpoints. They target the commercial AWS partition; other partitions need their documented endpoint suffix.

Load private qa_data/aws/s3.json containing nonempty string fields access_key and secret_key, plus session_token for temporary credentials. Prefer short-lived role credentials. These samples do not discover AWS profiles or refresh credentials. Set the bucket's actual region and replace sample bucket names and object keys. TLS certificate verification is explicitly enabled. Keep detailed HTTP diagnostics private because they can contain credentials or presigned URLs.

Downloads a small text object using the actual bucket region for both signing and endpoint selection. The complete object is held in memory. Empty objects are valid; do not treat an empty result as an error without checking LastMethodSuccess and HTTP status. Requires s3:GetObject and applicable KMS permissions. Use the file download example for binary or large objects.

AWS 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

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

    --  This example requires the Chilkat API to have been previously unlocked.
    --  See Global Unlock Sample for sample code.
    --  Load private credentials; never put access keys or session tokens in source.
    DECLARE @credentials int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @credentials OUT

    EXEC sp_OASetProperty @credentials, 'Strict', 1
    EXEC sp_OAMethod @credentials, 'LoadFile', @success OUT, 'qa_data/aws/s3.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load private S3 credentials.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @stringMatch1 int
    EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch1 OUT, '', 1
    EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
    IF (@iTmp0 <> 1) or (@stringMatch1 = 1)
      BEGIN

        PRINT 'Missing or invalid access_key.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
    EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @stringMatch2 int
    EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch2 OUT, '', 1
    EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'secret_key'
    IF (@iTmp0 <> 1) or (@stringMatch2 = 1)
      BEGIN

        PRINT 'Missing or invalid secret_key.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OASetProperty @http, 'AwsAccessKey', @sTmp0
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
    EXEC sp_OASetProperty @http, 'AwsSecretKey', @sTmp0
    EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
        EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
        DECLARE @stringMatch3 int
        EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch3 OUT, '', 1
        EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'session_token'
        IF (@iTmp0 <> 1) or (@stringMatch3 = 1)
          BEGIN

            PRINT 'session_token must be a nonempty string when supplied.'
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @http
            RETURN
          END
        EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
        EXEC sp_OASetProperty @http, 'AwsSessionToken', @sTmp0
      END
    --  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
    --  Set the actual bucket region, not the caller's location.
    DECLARE @region nvarchar(4000)
    SELECT @region = 'eu-central-1'
    EXEC sp_OASetProperty @http, 'AwsRegion', @region
    DECLARE @endpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT

    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 's3.'
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @region
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com'
    EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
    EXEC sp_OASetProperty @http, 'AwsEndpoint', @sTmp0
    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @http, 'FollowRedirects', 0
    --  A region redirect is not success. Correct the endpoint and signing region.
    DECLARE @bucketName nvarchar(4000)
    SELECT @bucketName = 'your-bucket-name'
    DECLARE @objectName nvarchar(4000)
    SELECT @objectName = 'hello.txt'

    --  Start with the original key. Encode once and retain '/' path separators.
    --  This also protects a literal "%2F" from being interpreted as a slash.
    DECLARE @keyPath int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @keyPath OUT

    EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @objectName
    EXEC sp_OAMethod @keyPath, 'Append', @success OUT, @sTmp0
    DECLARE @slashCount int
    EXEC sp_OAMethod @keyPath, 'ReplaceNoCase', @slashCount OUT, '%2f', '/'
    DECLARE @encodedKey nvarchar(4000)
    EXEC sp_OAMethod @keyPath, 'GetAsString', @encodedKey OUT
    DECLARE @text nvarchar(4000)
    EXEC sp_OAMethod @http, 'S3_DownloadString', @text OUT, @bucketName, @encodedKey, 'utf-8'
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN

        EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
        PRINT 'Request failed (transport or S3 error). HTTP status: ' + @iTmp0
        --  Inspect LastErrorText privately. Do not log credentials or signed URLs.
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @keyPath
        RETURN
      END
    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
        PRINT 'Unexpected HTTP status: ' + @iTmp0
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @keyPath
        RETURN
      END
    --  Empty content is valid. Success is determined by the call and HTTP status.

    PRINT @text

    EXEC @hr = sp_OADestroy @stringCompare
    EXEC @hr = sp_OADestroy @credentials
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @keyPath


END
GO