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

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.

AWS GetObject reference

Chilkat VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  StringBuilder compares text by value in every generated language.
set textCompare = CreateObject("Chilkat.StringBuilder")
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
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 = CreateObject("Chilkat.JsonObject")
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/s3.json")
If (success <> 1) Then
    outFile.WriteLine("Could not load private AWS credentials.")
    WScript.Quit
End If

textCompare.Clear 
success = textCompare.SetString(credentials.StringOf("access_key"))
sameText1 = textCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("access_key") <> 1) Or (sameText1 = 1)) Then
    outFile.WriteLine("Missing or invalid access_key.")
    WScript.Quit
End If

textCompare.Clear 
success = textCompare.SetString(credentials.StringOf("secret_key"))
sameText2 = textCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (sameText2 = 1)) Then
    outFile.WriteLine("Missing or invalid secret_key.")
    WScript.Quit
End If

set http = CreateObject("Chilkat.Http")
http.AwsAccessKey = credentials.StringOf("access_key")
http.AwsSecretKey = credentials.StringOf("secret_key")
http.AwsRegion = "us-west-2"
http.AwsEndpoint = "s3.us-west-2.amazonaws.com"
'  S3_GenPresignedUrl always generates a Signature Version 4 URL.
If (credentials.HasMember("session_token") = 1) Then
    If (credentials.JsonTypeOf("session_token") <> 1) Then
        outFile.WriteLine("session_token must be a string.")
        WScript.Quit
    End If

    http.AwsSessionToken = credentials.StringOf("session_token")
End If

'  Local URL generation; no S3 request is sent by this method.
signedUrl = http.S3_GenPresignedUrl("GET",1,"amzn-s3-demo-bucket","documents/example.txt",900,"s3")
If (http.LastMethodSuccess <> 1) Then
    outFile.WriteLine("Could not generate the presigned URL.")
    WScript.Quit
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.
outFile.WriteLine("Created an HTTPS download URL with a 15-minute requested lifetime.")

outFile.Close