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

Python
import sys
import chilkat2

success = False

# StringBuilder compares text by value in every generated language.
textCompare = chilkat2.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.
credentials = chilkat2.JsonObject()
credentials.Strict = True
success = credentials.LoadFile("qa_data/aws/s3.json")
if (success != True):
    print("Could not load private AWS credentials.")
    sys.exit()

textCompare.Clear()
textCompare.SetString(credentials.StringOf("access_key"))
sameText1 = textCompare.ContentsEqual("",True)
if ((credentials.JsonTypeOf("access_key") != 1) or (sameText1 == True)):
    print("Missing or invalid access_key.")
    sys.exit()

textCompare.Clear()
textCompare.SetString(credentials.StringOf("secret_key"))
sameText2 = textCompare.ContentsEqual("",True)
if ((credentials.JsonTypeOf("secret_key") != 1) or (sameText2 == True)):
    print("Missing or invalid secret_key.")
    sys.exit()

http = chilkat2.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):
    if (credentials.JsonTypeOf("session_token") != 1):
        print("session_token must be a string.")
        sys.exit()

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

# Local URL generation; no S3 request is sent by this method.
signedUrl = http.S3_GenPresignedUrl("GET",True,"amzn-s3-demo-bucket","documents/example.txt",900,"s3")
if (http.LastMethodSuccess != True):
    print("Could not generate the presigned URL.")
    sys.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.
print("Created an HTTPS download URL with a 15-minute requested lifetime.")