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

S3: Create a Bucket in a Region Using Http

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.

Creates a general purpose bucket through Chilkat.Http. Uses a short-lived SigV4 PUT URL and HttpStr to send the CreateBucketConfiguration XML to the matching regional endpoint. The same region drives signing, endpoint, and LocationConstraint. For us-east-1 it sends an empty body instead. The simpler S3_CreateBucket method sends an empty body and cannot supply a regional configuration.

Choose an unused bucket name; requires s3:CreateBucket. Presigned URLs are bearer credentials and must stay private. No public ACL or ownership override is sent. New-bucket security defaults apply. An uncertain response may mean the bucket was created; inspect its state before retrying. In us-east-1, recreating an owned bucket can reset its ACLs.

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-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 = 'replace-with-your-unique-bucket-name'
    DECLARE @config int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @config OUT

    EXEC sp_OASetProperty @config, 'Tag', 'CreateBucketConfiguration'
    EXEC sp_OAMethod @config, 'AddAttribute', @success OUT, 'xmlns', 'http://s3.amazonaws.com/doc/2006-03-01/'
    EXEC sp_OAMethod @config, 'UpdateChildContent', NULL, 'LocationConstraint', @region
    DECLARE @body nvarchar(4000)
    SELECT @body = ''
    EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @region
    DECLARE @stringMatch4 int
    EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch4 OUT, 'us-east-1', 1
    IF @stringMatch4 <> 1
      BEGIN
        EXEC sp_OAMethod @config, 'GetXml', @body OUT
      END
    --  A short-lived signed PUT URL lets Http send the required XML body.
    --  The signing region and regional endpoint are the SAME as LocationConstraint.
    DECLARE @signedUrl nvarchar(4000)
    EXEC sp_OAMethod @http, 'S3_GenPresignedUrl', @signedUrl OUT, 'PUT', 1, @bucketName, '', 300, 's3'
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'Could not sign bucket creation request.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @config
        RETURN
      END
    DECLARE @response int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @response OUT

    EXEC sp_OAMethod @http, 'HttpStr', @success OUT, 'PUT', @signedUrl, @body, 'utf-8', 'application/xml', @response
    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 @config
        EXEC @hr = sp_OADestroy @response
        RETURN
      END
    EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        EXEC sp_OAGetProperty @response, 'StatusCode', @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 @config
        EXEC @hr = sp_OADestroy @response
        RETURN
      END

    PRINT 'Regional bucket creation request 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 @config
    EXEC @hr = sp_OADestroy @response


END
GO