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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_TextCompare
oleobject loo_Credentials
long li_SameText1
long li_SameText2
oleobject loo_Http
string ls_SignedUrl
li_Success = 0
// StringBuilder compares text by value in every generated language.
loo_TextCompare = create oleobject
li_rc = loo_TextCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_TextCompare
MessageBox("Error","Connecting to COM object failed")
return
end if
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the S3 operation.
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 AWS credentials."
destroy loo_TextCompare
destroy loo_Credentials
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("access_key"))
li_SameText1 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_SameText1 = 1) then
Write-Debug "Missing or invalid access_key."
destroy loo_TextCompare
destroy loo_Credentials
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_SameText2 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_SameText2 = 1) then
Write-Debug "Missing or invalid secret_key."
destroy loo_TextCompare
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")
loo_Http.AwsRegion = "us-west-2"
loo_Http.AwsEndpoint = "s3.us-west-2.amazonaws.com"
// S3_GenPresignedUrl always generates a Signature Version 4 URL.
if loo_Credentials.HasMember("session_token") = 1 then
if loo_Credentials.JsonTypeOf("session_token") <> 1 then
Write-Debug "session_token must be a string."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Http
return
end if
loo_Http.AwsSessionToken = loo_Credentials.StringOf("session_token")
end if
// Local URL generation; no S3 request is sent by this method.
ls_SignedUrl = loo_Http.S3_GenPresignedUrl("GET",1,"amzn-s3-demo-bucket","documents/example.txt",900,"s3")
if loo_Http.LastMethodSuccess <> 1 then
Write-Debug "Could not generate the presigned URL."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Http
return
end if
// 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.
Write-Debug "Created an HTTPS download URL with a 15-minute requested lifetime."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Http