S3: Create and Use a Presigned Upload URL
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.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
set stringCompare [new_CkStringBuilder]
# 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.
set credentials [new_CkJsonObject]
CkJsonObject_put_Strict $credentials 1
set success [CkJsonObject_LoadFile $credentials "qa_data/aws/s3.json"]
if {$success != 1} then {
puts "Could not load private S3 credentials."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
exit
}
CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "access_key"]
set stringMatch1 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "access_key"] != 1] || [$stringMatch1 == 1]} then {
puts "Missing or invalid access_key."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
exit
}
CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "secret_key"]
set stringMatch2 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "secret_key"] != 1] || [$stringMatch2 == 1]} then {
puts "Missing or invalid secret_key."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
exit
}
set http [new_CkHttp]
CkHttp_put_AwsAccessKey $http [CkJsonObject_stringOf $credentials "access_key"]
CkHttp_put_AwsSecretKey $http [CkJsonObject_stringOf $credentials "secret_key"]
if {[CkJsonObject_HasMember $credentials "session_token"] == 1} then {
CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "session_token"]
set stringMatch3 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "session_token"] != 1] || [$stringMatch3 == 1]} then {
puts "session_token must be a nonempty string when supplied."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
exit
}
CkHttp_put_AwsSessionToken $http [CkJsonObject_stringOf $credentials "session_token"]
}
# Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
# Set the actual bucket region, not the caller's location.
set region "us-west-2"
CkHttp_put_AwsRegion $http $region
set endpoint [new_CkStringBuilder]
CkStringBuilder_Append $endpoint "s3."
CkStringBuilder_Append $endpoint $region
CkStringBuilder_Append $endpoint ".amazonaws.com"
CkHttp_put_AwsEndpoint $http [CkStringBuilder_getAsString $endpoint]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_FollowRedirects $http 0
# A region redirect is not success. Correct the endpoint and signing region.
set bucketName "your-bucket-name"
# A leading slash is part of a key. Do not add one to imitate a file path.
set 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.
set keyPath [new_CkStringBuilder]
CkStringBuilder_Append $keyPath [CkHttp_urlEncode $http $objectName]
set slashCount [CkStringBuilder_ReplaceNoCase $keyPath "%2f" "/"]
set encodedKey [CkStringBuilder_getAsString $keyPath]
set signedUrl [CkHttp_s3_GenPresignedUrl $http "PUT" 1 $bucketName $encodedKey 900 "s3"]
if {[CkHttp_get_LastMethodSuccess $http] != 1} then {
puts "Could not generate presigned upload URL."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $keyPath
exit
}
# 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.
set uploader [new_CkHttp]
CkHttp_put_RequireSslCertVerify $uploader 1
CkHttp_put_FollowRedirects $uploader 0
set response [new_CkHttpResponse]
set success [CkHttp_HttpFile $uploader "PUT" $signedUrl "qa_data/jpg/seahorse.jpg" "image/jpeg" $response]
if {$success != 1} then {
puts "Upload transport failed. Inspect diagnostics privately."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $keyPath
delete_CkHttp $uploader
delete_CkHttpResponse $response
exit
}
if {[CkHttpResponse_get_StatusCode $response] != 200} then {
puts "Unexpected HTTP status: [CkHttpResponse_get_StatusCode $response]"
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $keyPath
delete_CkHttp $uploader
delete_CkHttpResponse $response
exit
}
puts "Presigned upload succeeded."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $keyPath
delete_CkHttp $uploader
delete_CkHttpResponse $response