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

S3: Upload with Transfer Acceleration

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.

Generates a SigV4 URL valid for 15 minutes, using HTTPS and the bucket's actual signing region. A presigned URL is a bearer credential: protect it from shared logs and untrusted recipients. It remains usable multiple times until expiration or revocation; it is not a one-time URL. Temporary credentials may expire sooner, and bucket policies can further restrict use. SigV4 expiration cannot exceed seven days. Generation is local and does not verify the bucket, object, permissions, or credentials with S3.

The signer needs s3:PutObject. Uploading replaces an existing key or creates a new version. A separate Http instance demonstrates use without AWS credentials. This basic URL signs the host and does not bind Content-Type or a particular payload checksum; add a dedicated constrained-presigning example when those restrictions are needed.

Acceleration must already be enabled and supported in the bucket region. It requires virtual-hosted requests and a DNS-compatible bucket name without periods; additional transfer charges may apply. The endpoint changes to s3-accelerate.amazonaws.com, but the SigV4 service remains s3 and the signing region remains the bucket region. This sample does not enable acceleration.

AWS reference

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_Credentials
long li_StringMatch1
long li_StringMatch2
oleobject loo_Http
long li_StringMatch3
string ls_Region
oleobject loo_Endpoint
string ls_BucketName
string ls_ObjectName
oleobject loo_KeyPath
long li_SlashCount
string ls_EncodedKey
string ls_SignedUrl
oleobject loo_Uploader
oleobject loo_Response

li_Success = 0

loo_StringCompare = create oleobject
li_rc = loo_StringCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_StringCompare
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  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.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")

loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/s3.json")
if li_Success <> 1 then
    Write-Debug "Could not load private S3 credentials."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_StringCompare.SetString(loo_Credentials.StringOf("access_key"))
li_StringMatch1 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_StringMatch1 = 1) then
    Write-Debug "Missing or invalid access_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_StringCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_StringMatch2 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_StringMatch2 = 1) then
    Write-Debug "Missing or invalid secret_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.AwsAccessKey = loo_Credentials.StringOf("access_key")
loo_Http.AwsSecretKey = loo_Credentials.StringOf("secret_key")
if loo_Credentials.HasMember("session_token") = 1 then
    loo_StringCompare.SetString(loo_Credentials.StringOf("session_token"))
    li_StringMatch3 = loo_StringCompare.ContentsEqual("",1)
    if (loo_Credentials.JsonTypeOf("session_token") <> 1) OR (li_StringMatch3 = 1) then
        Write-Debug "session_token must be a nonempty string when supplied."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Http
        return
    end if

    loo_Http.AwsSessionToken = loo_Credentials.StringOf("session_token")
end if

//  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
//  Set the actual bucket region, not the caller's location.
ls_Region = "us-west-2"
loo_Http.AwsRegion = ls_Region
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")

loo_Endpoint.Append("s3.")
loo_Endpoint.Append(ls_Region)
loo_Endpoint.Append(".amazonaws.com")
loo_Http.AwsEndpoint = loo_Endpoint.GetAsString()
loo_Http.RequireSslCertVerify = 1
loo_Http.FollowRedirects = 0
//  A region redirect is not success. Correct the endpoint and signing region.
ls_BucketName = "your-bucket-name"
//  A leading slash is part of a key. Do not add one to imitate a file path.
ls_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.
loo_KeyPath = create oleobject
li_rc = loo_KeyPath.ConnectToNewObject("Chilkat.StringBuilder")

loo_KeyPath.Append(loo_Http.UrlEncode(ls_ObjectName))
li_SlashCount = loo_KeyPath.ReplaceNoCase("%2f","/")
ls_EncodedKey = loo_KeyPath.GetAsString()
//  Enable acceleration on this DNS-compatible, dot-free bucket first.
loo_Http.AwsEndpoint = "s3-accelerate.amazonaws.com"
ls_SignedUrl = loo_Http.S3_GenPresignedUrl("PUT",1,ls_BucketName,ls_EncodedKey,900,"s3")
if loo_Http.LastMethodSuccess <> 1 then
    Write-Debug "Could not generate presigned upload URL."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Http
    destroy loo_Endpoint
    destroy loo_KeyPath
    return
end if

//  A PUT URL is used by an HTTP upload client, not browser address-bar navigation.
//  The recipient needs this URL, not the signer's AWS credentials.
loo_Uploader = create oleobject
li_rc = loo_Uploader.ConnectToNewObject("Chilkat.Http")

loo_Uploader.RequireSslCertVerify = 1
loo_Uploader.FollowRedirects = 0
loo_Response = create oleobject
li_rc = loo_Response.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Uploader.HttpFile("PUT",ls_SignedUrl,"qa_data/jpg/seahorse.jpg","image/jpeg",loo_Response)
if li_Success <> 1 then
    Write-Debug "Upload transport failed. Inspect diagnostics privately."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Http
    destroy loo_Endpoint
    destroy loo_KeyPath
    destroy loo_Uploader
    destroy loo_Response
    return
end if

if loo_Response.StatusCode <> 200 then
    Write-Debug "Unexpected HTTP status: " + string(loo_Response.StatusCode)
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Http
    destroy loo_Endpoint
    destroy loo_KeyPath
    destroy loo_Uploader
    destroy loo_Response
    return
end if

Write-Debug "Presigned upload succeeded."


destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Uploader
destroy loo_Response