Amazon S3: Generate an HTTPS Presigned Download URL
See more S3 - REST Examples
Generate a short-lived HTTPS URL granting GetObject access without giving the recipient your secret key. This local signing example uses the Http presigning helper; the transfer examples in this category use Rest. No download is performed here. The signer must have permission to read the object.
A presigned URL is a bearer capability, reusable until expiration or loss of authorization. Signature Version 4 allows at most seven days with long-lived credentials; temporary credentials can expire sooner than the requested lifetime. Policy conditions can shorten access further. Use HTTPS and keep the complete URL private. The example requests 15 minutes and supports session credentials.
Load current credentials from a private qa_data/aws/s3.json file containing string fields access_key, secret_key, and session_token for temporary credentials. This example does not discover profiles or refresh expired credentials. The hostname shown is for a general purpose bucket in the commercial AWS partition.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# StringBuilder compares text by value in every generated language.
set textCompare [new_CkStringBuilder]
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0
# Private configuration, not a file in public web content or source control.
# Load current IAM credentials authorized for the S3 operation.
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 AWS credentials."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $credentials
exit
}
CkStringBuilder_Clear $textCompare
CkStringBuilder_SetString $textCompare [CkJsonObject_stringOf $credentials "access_key"]
set sameText1 [CkStringBuilder_ContentsEqual $textCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "access_key"] != 1] || [$sameText1 == 1]} then {
puts "Missing or invalid access_key."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $credentials
exit
}
CkStringBuilder_Clear $textCompare
CkStringBuilder_SetString $textCompare [CkJsonObject_stringOf $credentials "secret_key"]
set sameText2 [CkStringBuilder_ContentsEqual $textCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "secret_key"] != 1] || [$sameText2 == 1]} then {
puts "Missing or invalid secret_key."
delete_CkStringBuilder $textCompare
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"]
CkHttp_put_AwsRegion $http "us-west-2"
CkHttp_put_AwsEndpoint $http "s3.us-west-2.amazonaws.com"
# S3_GenPresignedUrl always generates a Signature Version 4 URL.
if {[CkJsonObject_HasMember $credentials "session_token"] == 1} then {
if {[CkJsonObject_JsonTypeOf $credentials "session_token"] != 1} then {
puts "session_token must be a string."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
exit
}
CkHttp_put_AwsSessionToken $http [CkJsonObject_stringOf $credentials "session_token"]
}
# Local URL generation; no S3 request is sent by this method.
set signedUrl [CkHttp_s3_GenPresignedUrl $http "GET" 1 "amzn-s3-demo-bucket" "documents/example.txt" 900 "s3"]
if {[CkHttp_get_LastMethodSuccess $http] != 1} then {
puts "Could not generate the presigned URL."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
exit
}
# Pass signedUrl directly to the intended recipient through a private channel.
# Do not print it to public logs, and do not modify its query or signed headers.
puts "Created an HTTPS download URL with a 15-minute requested lifetime."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $credentials
delete_CkHttp $http