S3: Upload with a Public-Read ACL (Legacy Buckets)
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.
Uploads a JPEG with the public-read canned ACL. This can grant anonymous read access and is unsuitable for private data.
Legacy ACL-enabled buckets only. New buckets disable ACLs by default, so public-read fails with AccessControlListNotSupported under Bucket owner enforced ownership. Applicable Block Public Access settings can reject or ignore public ACLs. This example does not change ownership or public-access settings. Prefer private objects and presigned downloads or an intentionally configured delivery policy.
Set the explicit opt-in flag only for a bucket already configured to permit this public ACL. Requires s3:PutObject and s3:PutObjectAcl. Successful upload alone does not prove anonymous access is effective.
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
-- Explicit opt-in for this legacy public ACL demonstration.
DECLARE @allowPublicRead int
SELECT @allowPublicRead = 0
IF @allowPublicRead <> 1
BEGIN
PRINT 'Public ACL example is disabled. Read its prerequisites before opting in.'
EXEC @hr = sp_OADestroy @stringCompare
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
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'x-amz-acl', 'public-read'
EXEC sp_OAMethod @http, 'S3_UploadFile', @success OUT, 'qa_data/jpg/seahorse.jpg', 'image/jpeg', @bucketName, @encodedKey
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
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
PRINT 'File upload succeeded.'
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