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

S3: Generate a Presigned Download URL

See more S3 - HTTP Examples

Uses Chilkat.Http with Amazon S3 general purpose buckets and Signature Version 4. These examples do not cover directory buckets/S3 Express, access-point ARNs, Outposts, or other specialized endpoints. They target the commercial AWS partition; other partitions need their documented endpoint suffix.

Load private qa_data/aws/s3.json containing nonempty string fields access_key and secret_key, plus session_token for temporary credentials. Prefer short-lived role credentials. These samples do not discover AWS profiles or refresh credentials. Set the bucket's actual region and replace sample bucket names and object keys. TLS certificate verification is explicitly enabled. Keep detailed HTTP diagnostics private because they can contain credentials or presigned URLs.

Generates a SigV4 URL valid for 15 minutes, using HTTPS and the bucket's actual signing region. A presigned URL is a bearer credential: protect it from shared logs and untrusted recipients. It remains usable multiple times until expiration or revocation; it is not a one-time URL. Temporary credentials may expire sooner, and bucket policies can further restrict use. SigV4 expiration cannot exceed seven days. Generation is local and does not verify the bucket, object, permissions, or credentials with S3.

The signer needs s3:GetObject. The URL is saved to qa_output/s3-download-url.txt; create and restrict that directory before running. Sharing the URL grants its bearer temporary download access without making the object public.

AWS reference

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoStringCompare
    Handle hoCredentials
    Boolean iStringMatch1
    Boolean iStringMatch2
    Handle hoHttp
    Boolean iStringMatch3
    String sRegion
    Handle hoEndpoint
    String sBucketName
    String sObjectName
    Handle hoKeyPath
    Integer iSlashCount
    String sEncodedKey
    String sSignedUrl
    Handle hoFiles
    String sUrlPath
    Handle hoUrlText
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
    If (Not(IsComObjectCreated(hoStringCompare))) Begin
        Send CreateComObject of hoStringCompare
    End
    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.
    //  Load private credentials; never put access keys or session tokens in source.
    Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
    If (Not(IsComObjectCreated(hoCredentials))) Begin
        Send CreateComObject of hoCredentials
    End
    Set ComStrict Of hoCredentials To True
    Get ComLoadFile Of hoCredentials "qa_data/aws/s3.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load private S3 credentials."
        Procedure_Return
    End

    Get ComStringOf Of hoCredentials "access_key" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch1
    Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch1 = True)) Begin
        Showln "Missing or invalid access_key."
        Procedure_Return
    End

    Get ComStringOf Of hoCredentials "secret_key" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch2
    Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch2 = True)) Begin
        Showln "Missing or invalid secret_key."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Get ComStringOf Of hoCredentials "access_key" To sTemp1
    Set ComAwsAccessKey Of hoHttp To sTemp1
    Get ComStringOf Of hoCredentials "secret_key" To sTemp1
    Set ComAwsSecretKey Of hoHttp To sTemp1
    Get ComHasMember Of hoCredentials "session_token" To bTemp1
    If (bTemp1 = True) Begin
        Get ComStringOf Of hoCredentials "session_token" To sTemp1
        Get ComSetString Of hoStringCompare sTemp1 To iSuccess
        Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
        Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
        If ((iTemp1 <> 1) Or (iStringMatch3 = True)) Begin
            Showln "session_token must be a nonempty string when supplied."
            Procedure_Return
        End

        Get ComStringOf Of hoCredentials "session_token" To sTemp1
        Set ComAwsSessionToken Of hoHttp To sTemp1
    End

    //  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
    //  Set the actual bucket region, not the caller's location.
    Move "us-west-2" To sRegion
    Set ComAwsRegion Of hoHttp To sRegion
    Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
    If (Not(IsComObjectCreated(hoEndpoint))) Begin
        Send CreateComObject of hoEndpoint
    End
    Get ComAppend Of hoEndpoint "s3." To iSuccess
    Get ComAppend Of hoEndpoint sRegion To iSuccess
    Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
    Get ComGetAsString Of hoEndpoint To sTemp1
    Set ComAwsEndpoint Of hoHttp To sTemp1
    Set ComRequireSslCertVerify Of hoHttp To True
    Set ComFollowRedirects Of hoHttp To False
    //  A region redirect is not success. Correct the endpoint and signing region.
    Move "your-bucket-name" To sBucketName
    //  A leading slash is part of a key. Do not add one to imitate a file path.
    Move "images/seahorse.jpg" To sObjectName

    //  Start with the original key. Encode once and retain '/' path separators.
    //  This also protects a literal "%2F" from being interpreted as a slash.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoKeyPath
    If (Not(IsComObjectCreated(hoKeyPath))) Begin
        Send CreateComObject of hoKeyPath
    End
    Get ComUrlEncode Of hoHttp sObjectName To sTemp1
    Get ComAppend Of hoKeyPath sTemp1 To iSuccess
    Get ComReplaceNoCase Of hoKeyPath "%2f" "/" To iSlashCount
    Get ComGetAsString Of hoKeyPath To sEncodedKey
    Get ComS3_GenPresignedUrl Of hoHttp "GET" True sBucketName sEncodedKey 900 "s3" To sSignedUrl
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Could not generate presigned download URL."
        Procedure_Return
    End

    //  Save the bearer URL in a private directory, not a shared application log.
    Get Create (RefClass(cComCkFileAccess)) To hoFiles
    If (Not(IsComObjectCreated(hoFiles))) Begin
        Send CreateComObject of hoFiles
    End
    Move "qa_output/s3-download-url.txt" To sUrlPath
    Get ComFileExists Of hoFiles sUrlPath To bTemp1
    If (bTemp1 = True) Begin
        Showln "Choose an unused private URL output path."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoUrlText
    If (Not(IsComObjectCreated(hoUrlText))) Begin
        Send CreateComObject of hoUrlText
    End
    Get ComAppend Of hoUrlText sSignedUrl To iSuccess
    Get ComWriteFile Of hoUrlText sUrlPath "utf-8" False To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not save the presigned URL."
        Procedure_Return
    End

    Showln "Presigned download URL saved to the private output file."


End_Procedure