Sample code for 30+ languages & platforms
VB.NET 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 VB.NET Downloads

VB.NET
Dim success As Boolean = False

'  StringBuilder compares text by value in every generated language.
Dim textCompare As New Chilkat.StringBuilder
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
'  Private configuration, not a file in public web content or source control.
'  Load current IAM credentials authorized for the S3 operation.
Dim credentials As New Chilkat.JsonObject
credentials.Strict = True
success = credentials.LoadFile("qa_data/aws/s3.json")
If (success <> True) Then
    Debug.WriteLine("Could not load private AWS credentials.")
    Exit Sub
End If

textCompare.Clear()
textCompare.SetString(credentials.StringOf("access_key"))
Dim sameText1 As Boolean = textCompare.ContentsEqual("",True)
If ((credentials.JsonTypeOf("access_key") <> 1) Or (sameText1 = True)) Then
    Debug.WriteLine("Missing or invalid access_key.")
    Exit Sub
End If

textCompare.Clear()
textCompare.SetString(credentials.StringOf("secret_key"))
Dim sameText2 As Boolean = textCompare.ContentsEqual("",True)
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (sameText2 = True)) Then
    Debug.WriteLine("Missing or invalid secret_key.")
    Exit Sub
End If

Dim http As New 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") = True) Then
    If (credentials.JsonTypeOf("session_token") <> 1) Then
        Debug.WriteLine("session_token must be a string.")
        Exit Sub
    End If

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

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