Amazon S3: Download an Object into BinData
See more S3 - REST Examples
Download object bytes with GetObject after checking response headers. Requires s3:GetObject, and potentially kms:Decrypt. Archived objects may need restoration first. HTTP 403 can mean insufficient permission, including a missing object when ListBucket is not allowed; it does not prove existence.
For small objects that fit comfortably in memory. Use streaming for large or untrusted-size responses. The byte-array variant is for language bindings that expose native bytes; BinData is the portable alternative.
For general purpose S3 buckets, using regional HTTPS endpoints and AWS Signature Version 4. Directory buckets (S3 Express), access points, Outposts, and other AWS partitions need their own endpoint/authentication rules. Use a bucket name without dots for these virtual-hosted TLS examples; see the path-style example for dotted names.
Provide current IAM credentials in a private qa_data/aws/s3.json file with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This file is explicitly loaded; these examples do not discover profiles or refresh credentials. Keep it outside public content and source control. The session token is included in the signed request. TLS certificate verification is enabled on the Socket used by Rest.
Transport success and HTTP success are different. Keep full diagnostics private; signed requests and presigned URLs can contain credentials or grant access. For retryable reads use bounded backoff with jitter. Before retrying a write with an uncertain outcome, check the resource state. Keep keys, upload IDs, version IDs, ETags, and large sizes as strings.
Chilkat SQL Server Downloads
-- 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
-- StringBuilder compares text by value in every generated language.
DECLARE @textCompare int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @textCompare OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
SELECT @success = 0
-- Private configuration, not a file in public web content or source control.
-- Load current IAM credentials authorized for the S3 operation.
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 AWS credentials.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
RETURN
END
EXEC sp_OAMethod @textCompare, 'Clear', NULL
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
DECLARE @sameText1 int
EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText1 OUT, '', 1
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
IF (@iTmp0 <> 1) or (@sameText1 = 1)
BEGIN
PRINT 'Missing or invalid access_key.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
RETURN
END
EXEC sp_OAMethod @textCompare, 'Clear', NULL
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
DECLARE @sameText2 int
EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText2 OUT, '', 1
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'secret_key'
IF (@iTmp0 <> 1) or (@sameText2 = 1)
BEGIN
PRINT 'Missing or invalid secret_key.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
RETURN
END
-- General purpose bucket in the commercial AWS partition.
DECLARE @region nvarchar(4000)
SELECT @region = 'us-west-2'
DECLARE @bucketName nvarchar(4000)
SELECT @bucketName = 'amzn-s3-demo-bucket'
DECLARE @endpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @bucketName
IF @success <> 1
BEGIN
PRINT 'Could not build text.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
RETURN
END
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.'
IF @success <> 1
BEGIN
PRINT 'Could not build text.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
RETURN
END
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 's3.'
IF @success <> 1
BEGIN
PRINT 'Could not build text.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
RETURN
END
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @region
IF @success <> 1
BEGIN
PRINT 'Could not build text.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
RETURN
END
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com'
IF @success <> 1
BEGIN
PRINT 'Could not build text.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
RETURN
END
DECLARE @aws int
EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @aws OUT
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
EXEC sp_OASetProperty @aws, 'AccessKey', @sTmp0
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
EXEC sp_OASetProperty @aws, 'SecretKey', @sTmp0
EXEC sp_OASetProperty @aws, 'Region', @region
EXEC sp_OASetProperty @aws, 'ServiceName', 's3'
EXEC sp_OASetProperty @aws, 'SignatureVersion', 4
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
EXEC sp_OASetProperty @rest, 'ConnectTimeoutMs', 20000
EXEC sp_OASetProperty @rest, 'IdleTimeoutMs', 60000
EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @aws
IF @success <> 1
BEGIN
PRINT 'Could not configure AWS signing.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
-- Temporary credentials require this token in every signed query.
-- Keep request URLs private. Do not clear this parameter between requests.
EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'session_token'
IF @iTmp0 <> 1
BEGIN
PRINT 'session_token must be a string.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
EXEC sp_OAMethod @textCompare, 'Clear', NULL
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
DECLARE @sameText3 int
EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText3 OUT, '', 1
IF @sameText3 <> 1
BEGIN
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not prepare the request.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
END
END
-- Connect to the SAME hostname used for TLS verification and request signing.
DECLARE @connection int
EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT
EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @connection, 'MaxReadIdleMs', 60000
EXEC sp_OASetProperty @connection, 'MaxSendIdleMs', 60000
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
IF @success <> 1
BEGIN
PRINT 'TLS connection failed; inspect diagnostics privately.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 1
IF @success <> 1
BEGIN
PRINT 'Could not attach the TLS connection.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
DECLARE @xml int
EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT
EXEC sp_OASetProperty @xml, 'Strict', 1
-- Supply an unencoded object key, not a URL. Preserve slashes in the key.
DECLARE @objectKey nvarchar(4000)
SELECT @objectKey = 'documents/example.txt'
DECLARE @objectPath int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @objectPath OUT
EXEC sp_OAMethod @objectPath, 'Append', @success OUT, @objectKey
IF @success <> 1
BEGIN
PRINT 'Could not build text.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @objectPath
RETURN
END
EXEC sp_OAMethod @objectPath, 'Encode', @success OUT, 'url_rfc3986', 'utf-8'
IF @success <> 1
BEGIN
PRINT 'Could not encode the object key.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @objectPath
RETURN
END
DECLARE @restoredSlashes int
EXEC sp_OAMethod @objectPath, 'Replace', @restoredSlashes OUT, '%2F', '/'
EXEC sp_OAMethod @objectPath, 'Prepend', @success OUT, '/'
IF @success <> 1
BEGIN
PRINT 'Could not prepare the request.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @objectPath
RETURN
END
EXEC sp_OAMethod @objectPath, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @rest, 'SendReqNoBody', @success OUT, 'GET', @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not send download request.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @objectPath
RETURN
END
DECLARE @statusCode int
EXEC sp_OAMethod @rest, 'ReadResponseHeader', @statusCode OUT
IF @statusCode < 0
BEGIN
PRINT 'Could not read download headers.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @objectPath
RETURN
END
IF @statusCode <> 200
BEGIN
PRINT 'Download HTTP status: ' + @statusCode
DECLARE @errorBody nvarchar(4000)
EXEC sp_OAMethod @rest, 'ReadRespBodyString', @errorBody OUT
EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @errorBody
EXEC sp_OAMethod @textCompare, 'Clear', NULL
EXEC sp_OAGetProperty @xml, 'Tag', @sTmp0 OUT
EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
DECLARE @sameText4 int
EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText4 OUT, 'Error', 1
IF (@success = 1) and (@sameText4 = 1)
BEGIN
EXEC sp_OAMethod @xml, 'GetChildContent', @sTmp0 OUT, 'Code'
PRINT 'S3 error code: ' + @sTmp0
END
END
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @objectPath
RETURN
END
DECLARE @data int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @data OUT
EXEC sp_OAMethod @rest, 'ReadRespBd', @success OUT, @data
IF @success <> 1
BEGIN
PRINT 'Could not read download bytes.'
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @objectPath
EXEC @hr = sp_OADestroy @data
RETURN
END
EXEC sp_OAGetProperty @data, 'NumBytes', @iTmp0 OUT
PRINT 'Downloaded bytes: ' + @iTmp0
EXEC @hr = sp_OADestroy @textCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @objectPath
EXEC @hr = sp_OADestroy @data
END
GO