S3: List Objects Under a Prefix
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.
Uses S3_ListBucketObjects with list-type=2 to request ListObjectsV2. Follows opaque NextContinuationToken values while IsTruncated is true, including empty pages; detects missing/repeated tokens and stops at 10,000 pages with incomplete-results status. Requires s3:ListBucket.
Requests encoding-type=url so keys containing XML-incompatible characters can be represented. Decode returned keys exactly once before reusing them. Query values are encoded separately. Sizes remain decimal strings to preserve large values; LastModified is an ISO 8601 UTC timestamp, not RFC 822. Owner.DisplayName is not used. Listing pages are not a single snapshot if objects change during enumeration.
A prefix is a key-name filter, not a filesystem folder. The trailing slash matters: images/ excludes keys such as images-old/file.jpg. Without a delimiter, all matching descendant keys are included.
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
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'
DECLARE @bucketPath int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @bucketPath OUT
DECLARE @encoded int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @encoded OUT
DECLARE @xml int
EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT
DECLARE @seenTokens int
EXEC @hr = sp_OACreate 'Chilkat.Hashtable', @seenTokens OUT
DECLARE @token nvarchar(4000)
SELECT @token = ''
DECLARE @more int
SELECT @more = 1
DECLARE @page int
SELECT @page = 0
DECLARE @prefix nvarchar(4000)
SELECT @prefix = 'images/'
WHILE (@more = 1)
BEGIN
IF @page = 10000
BEGIN
PRINT 'Page limit reached; listing is incomplete.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
SELECT @page = @page + 1
EXEC sp_OAMethod @bucketPath, 'SetString', @success OUT, @bucketName
EXEC sp_OAMethod @bucketPath, 'Append', @success OUT, '?list-type=2&max-keys=1000&encoding-type=url'
EXEC sp_OAMethod @encoded, 'SetString', @success OUT, @prefix
EXEC sp_OAMethod @bucketPath, 'Append', @success OUT, '&prefix='
EXEC sp_OAMethod @encoded, 'GetEncoded', @sTmp0 OUT, 'url', 'utf-8'
EXEC sp_OAMethod @bucketPath, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @token
DECLARE @stringMatch4 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch4 OUT, '', 1
IF @stringMatch4 <> 1
BEGIN
EXEC sp_OAMethod @encoded, 'SetString', @success OUT, @token
EXEC sp_OAMethod @bucketPath, 'Append', @success OUT, '&continuation-token='
EXEC sp_OAMethod @encoded, 'GetEncoded', @sTmp0 OUT, 'url', 'utf-8'
EXEC sp_OAMethod @bucketPath, 'Append', @success OUT, @sTmp0
END
DECLARE @xmlStr nvarchar(4000)
EXEC sp_OAMethod @bucketPath, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'S3_ListBucketObjects', @xmlStr OUT, @sTmp0
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 @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
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 @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @xmlStr
IF @success <> 1
BEGIN
PRINT 'Invalid XML response.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
EXEC sp_OAGetProperty @xml, 'Tag', @sTmp0 OUT
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch5 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch5 OUT, 'ListBucketResult', 1
IF @stringMatch5 <> 1
BEGIN
PRINT 'Unexpected XML response root.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
EXEC sp_OAMethod @xml, 'GetChildContent', @sTmp0 OUT, 'EncodingType'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch6 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch6 OUT, 'url', 1
IF @stringMatch6 <> 1
BEGIN
PRINT 'Expected URL-encoded listing keys.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
DECLARE @i int
SELECT @i = 0
DECLARE @count int
EXEC sp_OAMethod @xml, 'NumChildrenHavingTag', @count OUT, 'Contents'
WHILE (@i < @count)
BEGIN
EXEC sp_OASetProperty @xml, 'I', @i
-- Decode keys exactly once. Sizes remain decimal strings (not int).
DECLARE @itemKey nvarchar(4000)
EXEC sp_OAMethod @xml, 'GetChildContent', @sTmp0 OUT, 'Contents[i]|Key'
EXEC sp_OAMethod @http, 'UrlDecode', @itemKey OUT, @sTmp0
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @itemKey
DECLARE @stringMatch7 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch7 OUT, '', 1
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF (@iTmp0 <> 1) or (@stringMatch7 = 1)
BEGIN
PRINT 'Invalid object key.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
DECLARE @size nvarchar(4000)
EXEC sp_OAMethod @xml, 'GetChildContent', @size OUT, 'Contents[i]|Size'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @size
DECLARE @stringMatch8 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch8 OUT, '', 1
EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
IF (@iTmp0 <> 1) or (@stringMatch8 = 1)
BEGIN
PRINT 'Incomplete object entry.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
DECLARE @modified nvarchar(4000)
EXEC sp_OAMethod @xml, 'GetChildContent', @modified OUT, 'Contents[i]|LastModified'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @modified
DECLARE @stringMatch9 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch9 OUT, '', 1
EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
IF (@iTmp0 <> 1) or (@stringMatch9 = 1)
BEGIN
PRINT 'Incomplete object entry.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
PRINT 'Key: ' + @itemKey
PRINT 'Bytes: ' + @size
-- S3 returns ISO 8601, not RFC 822. Keep the original UTC timestamp.
PRINT 'Last modified (UTC): ' + @modified
SELECT @i = @i + 1
END
DECLARE @truncated nvarchar(4000)
EXEC sp_OAMethod @xml, 'GetChildContent', @truncated OUT, 'IsTruncated'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @truncated
DECLARE @stringMatch10 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch10 OUT, 'true', 1
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @truncated
DECLARE @stringMatch11 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch11 OUT, 'false', 1
IF (@stringMatch10 <> 1) and (@stringMatch11 <> 1)
BEGIN
PRINT 'Missing or invalid IsTruncated; listing is incomplete.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
SELECT @more = 0
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @truncated
DECLARE @stringMatch12 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch12 OUT, 'true', 1
IF @stringMatch12 = 1
BEGIN
EXEC sp_OAMethod @xml, 'GetChildContent', @token OUT, 'NextContinuationToken'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @token
DECLARE @stringMatch13 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch13 OUT, '', 1
EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
IF (@iTmp0 <> 1) or (@stringMatch13 = 1)
BEGIN
PRINT 'Missing continuation token; listing is incomplete.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
EXEC sp_OAMethod @seenTokens, 'Contains', @iTmp0 OUT, @token
IF @iTmp0 = 1
BEGIN
PRINT 'Repeated continuation token; listing is incomplete.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
EXEC sp_OAMethod @seenTokens, 'AddStr', @success OUT, @token, 'seen'
IF @success <> 1
BEGIN
PRINT 'Could not track pagination.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
RETURN
END
SELECT @more = 1
END
END
PRINT 'Object listing complete.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @bucketPath
EXEC @hr = sp_OADestroy @encoded
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @seenTokens
END
GO