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

S3: Download a Specific Object Version

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 the exact key to an unused temporary path, requires HTTP 200, compares the local byte count with Content-Length, and renames only after success. Create the output directory first. Interrupted or failed downloads are not promoted to the final path. This is not a resumable download and does not independently verify an S3 checksum; ETag must not be assumed to be MD5.

Requires s3:GetObject, or s3:GetObjectVersion for a specified version, plus applicable KMS permissions. Archived S3 Glacier Flexible Retrieval or Deep Archive objects must be restored before reading; a restoration request does not immediately make them downloadable. Use the original key without adding a leading slash.

The opaque version ID is URL-encoded as a query value, not concatenated unescaped. A delete marker is not downloadable object content. AwsSubResources belongs to this Http instance; clear it before reusing the instance for requests that should not select the same version.

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 @iTmp1 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 = 'us-west-2'
    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'
    --  A leading slash is part of a key. Do not add one to imitate a file path.
    DECLARE @objectName nvarchar(4000)
    SELECT @objectName = 'images/seahorse.jpg'

    --  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 @versionId nvarchar(4000)
    SELECT @versionId = 'YOUR_VERSION_ID'
    DECLARE @subresource int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @subresource OUT

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

    EXEC sp_OAMethod @encodedVersion, 'Append', @success OUT, @versionId
    EXEC sp_OAMethod @subresource, 'Append', @success OUT, 'versionId='
    EXEC sp_OAMethod @encodedVersion, 'GetEncoded', @sTmp0 OUT, 'url', 'utf-8'
    EXEC sp_OAMethod @subresource, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @subresource, 'GetAsString', @sTmp0 OUT
    EXEC sp_OASetProperty @http, 'AwsSubResources', @sTmp0
    DECLARE @files int
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @files OUT

    DECLARE @finalPath nvarchar(4000)
    SELECT @finalPath = 'qa_output/seahorse.jpg'
    DECLARE @partialPath nvarchar(4000)
    SELECT @partialPath = 'qa_output/seahorse.jpg.part'
    EXEC sp_OAMethod @files, 'FileExists', @iTmp0 OUT, @finalPath
    EXEC sp_OAMethod @files, 'FileExists', @iTmp1 OUT, @partialPath
    IF (@iTmp0 = 1) or (@iTmp1 = 1)
      BEGIN

        PRINT 'Choose unused output paths before downloading.'
        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
        EXEC @hr = sp_OADestroy @subresource
        EXEC @hr = sp_OADestroy @encodedVersion
        EXEC @hr = sp_OADestroy @files
        RETURN
      END
    EXEC sp_OAMethod @http, 'S3_DownloadFile', @success OUT, @bucketName, @encodedKey, @partialPath
    IF @success <> 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
        EXEC @hr = sp_OADestroy @subresource
        EXEC @hr = sp_OADestroy @encodedVersion
        EXEC @hr = sp_OADestroy @files
        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
        EXEC @hr = sp_OADestroy @subresource
        EXEC @hr = sp_OADestroy @encodedVersion
        EXEC @hr = sp_OADestroy @files
        RETURN
      END
    DECLARE @headers int
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @headers OUT

    EXEC sp_OAGetProperty @http, 'LastResponseHeader', @sTmp0 OUT
    EXEC sp_OAMethod @headers, 'LoadMime', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not parse download headers; keep the .part file.'
        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
        EXEC @hr = sp_OADestroy @subresource
        EXEC @hr = sp_OADestroy @encodedVersion
        EXEC @hr = sp_OADestroy @files
        EXEC @hr = sp_OADestroy @headers
        RETURN
      END
    DECLARE @expectedSize nvarchar(4000)
    EXEC sp_OAMethod @headers, 'GetHeaderField', @expectedSize OUT, 'Content-Length'
    EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @expectedSize
    DECLARE @stringMatch4 int
    EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch4 OUT, '', 1
    EXEC sp_OAGetProperty @headers, 'LastMethodSuccess', @iTmp0 OUT
    IF (@iTmp0 <> 1) or (@stringMatch4 = 1)
      BEGIN

        PRINT 'Missing Content-Length; keep the .part file.'
        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
        EXEC @hr = sp_OADestroy @subresource
        EXEC @hr = sp_OADestroy @encodedVersion
        EXEC @hr = sp_OADestroy @files
        EXEC @hr = sp_OADestroy @headers
        RETURN
      END
    DECLARE @actualSize nvarchar(4000)
    EXEC sp_OAMethod @files, 'FileSizeStr', @actualSize OUT, @partialPath
    EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @actualSize
    DECLARE @stringMatch5 int
    EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch5 OUT, @expectedSize, 1
    EXEC sp_OAGetProperty @files, 'LastMethodSuccess', @iTmp0 OUT
    IF (@iTmp0 <> 1) or (@stringMatch5 <> 1)
      BEGIN

        PRINT 'Downloaded byte count mismatch; keep the .part file.'
        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
        EXEC @hr = sp_OADestroy @subresource
        EXEC @hr = sp_OADestroy @encodedVersion
        EXEC @hr = sp_OADestroy @files
        EXEC @hr = sp_OADestroy @headers
        RETURN
      END
    EXEC sp_OAMethod @files, 'FileRename', @success OUT, @partialPath, @finalPath
    IF @success <> 1
      BEGIN

        PRINT 'Downloaded file remains at the .part path; rename failed.'
        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
        EXEC @hr = sp_OADestroy @subresource
        EXEC @hr = sp_OADestroy @encodedVersion
        EXEC @hr = sp_OADestroy @files
        EXEC @hr = sp_OADestroy @headers
        RETURN
      END

    PRINT 'File download succeeded; byte count verified.'

    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
    EXEC @hr = sp_OADestroy @subresource
    EXEC @hr = sp_OADestroy @encodedVersion
    EXEC @hr = sp_OADestroy @files
    EXEC @hr = sp_OADestroy @headers


END
GO