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.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loCredentials
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL loHttp
LOCAL lnStringMatch3
LOCAL lcRegion
LOCAL loEndpoint
LOCAL lcBucketName
LOCAL lcObjectName
LOCAL loKeyPath
LOCAL lnSlashCount
LOCAL lcEncodedKey
LOCAL lcSignedUrl
LOCAL loUploader
LOCAL loResponse
lnSuccess = 0
loStringCompare = CreateObject('Chilkat.StringBuilder')
* 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.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/s3.json")
IF (lnSuccess <> 1) THEN
? "Could not load private S3 credentials."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loStringCompare.SetString(loCredentials.StringOf("access_key"))
lnStringMatch1 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (lnStringMatch1 = 1)) THEN
? "Missing or invalid access_key."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loStringCompare.SetString(loCredentials.StringOf("secret_key"))
lnStringMatch2 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (lnStringMatch2 = 1)) THEN
? "Missing or invalid secret_key."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loHttp = CreateObject('Chilkat.Http')
loHttp.AwsAccessKey = loCredentials.StringOf("access_key")
loHttp.AwsSecretKey = loCredentials.StringOf("secret_key")
IF (loCredentials.HasMember("session_token") = 1) THEN
loStringCompare.SetString(loCredentials.StringOf("session_token"))
lnStringMatch3 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("session_token") <> 1) OR (lnStringMatch3 = 1)) THEN
? "session_token must be a nonempty string when supplied."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
CANCEL
ENDIF
loHttp.AwsSessionToken = loCredentials.StringOf("session_token")
ENDIF
* Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
* Set the actual bucket region, not the caller's location.
lcRegion = "us-west-2"
loHttp.AwsRegion = lcRegion
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("s3.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loHttp.AwsEndpoint = loEndpoint.GetAsString()
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
* A region redirect is not success. Correct the endpoint and signing region.
lcBucketName = "your-bucket-name"
* A leading slash is part of a key. Do not add one to imitate a file path.
lcObjectName = "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.
loKeyPath = CreateObject('Chilkat.StringBuilder')
loKeyPath.Append(loHttp.UrlEncode(lcObjectName))
lnSlashCount = loKeyPath.ReplaceNoCase("%2f","/")
lcEncodedKey = loKeyPath.GetAsString()
* Enable acceleration on this DNS-compatible, dot-free bucket first.
loHttp.AwsEndpoint = "s3-accelerate.amazonaws.com"
lcSignedUrl = loHttp.S3_GenPresignedUrl("PUT",1,lcBucketName,lcEncodedKey,900,"s3")
IF (loHttp.LastMethodSuccess <> 1) THEN
? "Could not generate presigned upload URL."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loKeyPath
CANCEL
ENDIF
* 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.
loUploader = CreateObject('Chilkat.Http')
loUploader.RequireSslCertVerify = 1
loUploader.FollowRedirects = 0
loResponse = CreateObject('Chilkat.HttpResponse')
lnSuccess = loUploader.HttpFile("PUT",lcSignedUrl,"qa_data/jpg/seahorse.jpg","image/jpeg",loResponse)
IF (lnSuccess <> 1) THEN
? "Upload transport failed. Inspect diagnostics privately."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loKeyPath
RELEASE loUploader
RELEASE loResponse
CANCEL
ENDIF
IF (loResponse.StatusCode <> 200) THEN
? "Unexpected HTTP status: " + STR(loResponse.StatusCode)
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loKeyPath
RELEASE loUploader
RELEASE loResponse
CANCEL
ENDIF
? "Presigned upload succeeded."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loKeyPath
RELEASE loUploader
RELEASE loResponse