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

S3 Http: Download and Verify a Full-Object SHA-256 Checksum

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.

Uses explicit signed Http requests to preserve query parameters, conditional headers, and full responses. The included SigV4 block signs the exact path, sorted encoded query and headers used by this example; it is not a general-purpose HTTP signing library. Keep the clock accurate and do not change signed data after signing. Code requires Chilkat 11.6.0 or later; locally tested with 11.8.0.

For objects uploaded with a FULL_OBJECT SHA-256 checksum, reads HEAD checksum metadata, pins a returned version ID and ETag, downloads using If-Match, and checks returned metadata, actual byte count and the locally computed base64 SHA-256 before renaming. Empty objects are supported. Create qa_output first; existing output paths are not overwritten.

COMPOSITE or absent SHA-256 checksums stop explicitly. Use the companion single-PUT checksum upload to create a suitable object. Requires s3:GetObject/s3:GetObjectVersion and applicable KMS permissions. No multipart ETag-as-MD5 assumption.

AWS reference

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

$oStringCompare = ObjCreate("Chilkat.StringBuilder")
;  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.
$oCredentials = ObjCreate("Chilkat.JsonObject")
$oCredentials.Strict = True
$bSuccess = $oCredentials.LoadFile("qa_data/aws/s3.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load private S3 credentials." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($oCredentials.StringOf("access_key"))
Local $bStringMatch1 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("access_key") <> 1) Or ($bStringMatch1 = True)) Then
    ConsoleWrite("Missing or invalid access_key." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($oCredentials.StringOf("secret_key"))
Local $bStringMatch2 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("secret_key") <> 1) Or ($bStringMatch2 = True)) Then
    ConsoleWrite("Missing or invalid secret_key." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AwsAccessKey = $oCredentials.StringOf("access_key")
$oHttp.AwsSecretKey = $oCredentials.StringOf("secret_key")
If ($oCredentials.HasMember("session_token") = True) Then
    $oStringCompare.SetString($oCredentials.StringOf("session_token"))
Local $bStringMatch3 = $oStringCompare.ContentsEqual("",True)
    If (($oCredentials.JsonTypeOf("session_token") <> 1) Or ($bStringMatch3 = True)) Then
        ConsoleWrite("session_token must be a nonempty string when supplied." & @CRLF)
        Exit
    EndIf

    $oHttp.AwsSessionToken = $oCredentials.StringOf("session_token")
EndIf

;  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
;  Set the actual bucket region, not the caller's location.
Local $sRegion = "us-west-2"
$oHttp.AwsRegion = $sRegion
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$oEndpoint.Append("s3.")
$oEndpoint.Append($sRegion)
$oEndpoint.Append(".amazonaws.com")
$oHttp.AwsEndpoint = $oEndpoint.GetAsString()
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
;  A region redirect is not success. Correct the endpoint and signing region.
Local $sBucketName = "your-bucket-name"
Local $sObjectKey = "examples/archive.bin"
$oResponse = ObjCreate("Chilkat.HttpResponse")
$oSigner = ObjCreate("Chilkat.Crypt2")
$oSigner.Charset = "utf-8"
$oSigner.HashAlgorithm = "sha256"
$oSigner.EncodingMode = "hexlower"
$oSigner.MacAlgorithm = "hmac"
$oNow = ObjCreate("Chilkat.CkDateTime")
$oTimestamp = ObjCreate("Chilkat.StringBuilder")
$oCanonical = ObjCreate("Chilkat.StringBuilder")
$oCanonicalHeaders = ObjCreate("Chilkat.StringBuilder")
$oSignedHeaders = ObjCreate("Chilkat.StringBuilder")
$oScope = ObjCreate("Chilkat.StringBuilder")
$oStringToSign = ObjCreate("Chilkat.StringBuilder")
$oSigningKey = ObjCreate("Chilkat.StringBuilder")
$oAuthorization = ObjCreate("Chilkat.StringBuilder")
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oQuery = ObjCreate("Chilkat.StringBuilder")
$oNumber = ObjCreate("Chilkat.StringBuilder")
$oHost = ObjCreate("Chilkat.StringBuilder")
$oUri = ObjCreate("Chilkat.StringBuilder")
$oXml = ObjCreate("Chilkat.Xml")
Local $sAmzDate
Local $sDateStamp
Local $sPayloadHash
Local $sCanonicalHash
Local $sDerivedKey
Local $signature
Local $iReplaceCount
$oHost.Append($sBucketName)
$oHost.Append(".")
$oHost.Append($oEndpoint.GetAsString())
;  Encode the original key exactly once, preserving S3 path separators.
$oUri.SetString($oHttp.UrlEncode($sObjectKey))
$iReplaceCount = $oUri.ReplaceNoCase("%2f","/")
If (($oUri.StartsWith("/",True) = True) Or ($oUri.Contains("/../",True) = True) Or ($oUri.Contains("/./",True) = True)) Then
    ConsoleWrite("Use a key without a leading slash or dot path segments for these examples." & @CRLF)
    Exit
EndIf

$oUri.Prepend("/")
Local $sVersionId = ""
Local $sEtag
Local $sExpectedSize
Local $sExpectedChecksum
Local $sChecksumType
Local $sReturnedVersion
$oFiles = ObjCreate("Chilkat.FileAccess")
Local $sFinalPath = "qa_output/s3-download.bin"
Local $sPartialPath = "qa_output/s3-download.bin.part"
If (($oFiles.FileExists($sFinalPath) = True) Or ($oFiles.FileExists($sPartialPath) = True)) Then
    ConsoleWrite("Choose unused output paths." & @CRLF)
    Exit
EndIf

$oQuery.Clear 
$oStringCompare.SetString($sVersionId)
Local $bStringMatch4 = $oStringCompare.ContentsEqual("",True)
If ($bStringMatch4 <> True) Then
    If ($oQuery.Length > 0) Then
        $oQuery.Append("&")
    EndIf

    $oQuery.Append("versionId=")
    $oQuery.Append($oHttp.UrlEncode($sVersionId))
EndIf

$oHttp.ClearHeaders 
$bSuccess = $oNow.SetFromCurrentSystemTime()
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read system time." & @CRLF)
    Exit
EndIf

$oTimestamp.SetString($oNow.GetAsTimestamp(False))
$iReplaceCount = $oTimestamp.Replace("-","")
$iReplaceCount = $oTimestamp.Replace(":","")
$sAmzDate = $oTimestamp.GetAsString()
$sDateStamp = $oTimestamp.GetRange(0,8,False)
$sPayloadHash = $oSigner.HashStringENC("")
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not hash payload." & @CRLF)
    Exit
EndIf

$oCanonicalHeaders.Clear 
$oSignedHeaders.Clear 
If ($oSignedHeaders.Length > 0) Then
    $oSignedHeaders.Append(";")
EndIf

$oSignedHeaders.Append("host")
$oCanonicalHeaders.Append("host:")
$oCanonicalHeaders.Append($oHost.GetAsString())
$oCanonicalHeaders.Append(@LF)
$oHttp.SetRequestHeader "host",$oHost.GetAsString()
If ($oSignedHeaders.Length > 0) Then
    $oSignedHeaders.Append(";")
EndIf

$oSignedHeaders.Append("x-amz-checksum-mode")
$oCanonicalHeaders.Append("x-amz-checksum-mode:")
$oCanonicalHeaders.Append("ENABLED")
$oCanonicalHeaders.Append(@LF)
$oHttp.SetRequestHeader "x-amz-checksum-mode","ENABLED"
If ($oSignedHeaders.Length > 0) Then
    $oSignedHeaders.Append(";")
EndIf

$oSignedHeaders.Append("x-amz-content-sha256")
$oCanonicalHeaders.Append("x-amz-content-sha256:")
$oCanonicalHeaders.Append($sPayloadHash)
$oCanonicalHeaders.Append(@LF)
$oHttp.SetRequestHeader "x-amz-content-sha256",$sPayloadHash
If ($oSignedHeaders.Length > 0) Then
    $oSignedHeaders.Append(";")
EndIf

$oSignedHeaders.Append("x-amz-date")
$oCanonicalHeaders.Append("x-amz-date:")
$oCanonicalHeaders.Append($sAmzDate)
$oCanonicalHeaders.Append(@LF)
$oHttp.SetRequestHeader "x-amz-date",$sAmzDate
If ($oCredentials.HasMember("session_token") = True) Then
    If ($oSignedHeaders.Length > 0) Then
        $oSignedHeaders.Append(";")
    EndIf

    $oSignedHeaders.Append("x-amz-security-token")
    $oCanonicalHeaders.Append("x-amz-security-token:")
    $oCanonicalHeaders.Append($oCredentials.StringOf("session_token"))
    $oCanonicalHeaders.Append(@LF)
    $oHttp.SetRequestHeader "x-amz-security-token",$oCredentials.StringOf("session_token")
EndIf

$oCanonical.Clear 
$oCanonical.Append("HEAD" & @LF)
$oCanonical.Append($oUri.GetAsString())
$oCanonical.Append(@LF)
$oCanonical.Append($oQuery.GetAsString())
$oCanonical.Append(@LF)
$oCanonical.Append($oCanonicalHeaders.GetAsString())
$oCanonical.Append(@LF)
$oCanonical.Append($oSignedHeaders.GetAsString())
$oCanonical.Append(@LF)
$oCanonical.Append($sPayloadHash)
$sCanonicalHash = $oSigner.HashStringENC($oCanonical.GetAsString())
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not hash canonical request." & @CRLF)
    Exit
EndIf

$oScope.Clear 
$oScope.Append($sDateStamp)
$oScope.Append("/")
$oScope.Append($sRegion)
$oScope.Append("/s3/aws4_request")
$oStringToSign.Clear 
$oStringToSign.Append("AWS4-HMAC-SHA256" & @LF)
$oStringToSign.Append($sAmzDate)
$oStringToSign.Append(@LF)
$oStringToSign.Append($oScope.GetAsString())
$oStringToSign.Append(@LF)
$oStringToSign.Append($sCanonicalHash)
$oSigningKey.SetString("AWS4")
$oSigningKey.Append($oCredentials.StringOf("secret_key"))
$bSuccess = $oSigner.SetMacKeyString($oSigningKey.GetAsString())
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set signing key." & @CRLF)
    Exit
EndIf

$sDerivedKey = $oSigner.MacStringENC($sDateStamp)
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not derive signing key." & @CRLF)
    Exit
EndIf

$bSuccess = $oSigner.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set signing key." & @CRLF)
    Exit
EndIf

$sDerivedKey = $oSigner.MacStringENC($sRegion)
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not derive signing key." & @CRLF)
    Exit
EndIf

$bSuccess = $oSigner.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set signing key." & @CRLF)
    Exit
EndIf

$sDerivedKey = $oSigner.MacStringENC("s3")
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not derive signing key." & @CRLF)
    Exit
EndIf

$bSuccess = $oSigner.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set signing key." & @CRLF)
    Exit
EndIf

$sDerivedKey = $oSigner.MacStringENC("aws4_request")
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not derive signing key." & @CRLF)
    Exit
EndIf

$bSuccess = $oSigner.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set signing key." & @CRLF)
    Exit
EndIf

$signature = $oSigner.MacStringENC($oStringToSign.GetAsString())
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not sign request." & @CRLF)
    Exit
EndIf

$oAuthorization.Clear 
$oAuthorization.Append("AWS4-HMAC-SHA256 Credential=")
$oAuthorization.Append($oCredentials.StringOf("access_key"))
$oAuthorization.Append("/")
$oAuthorization.Append($oScope.GetAsString())
$oAuthorization.Append(", SignedHeaders=")
$oAuthorization.Append($oSignedHeaders.GetAsString())
$oAuthorization.Append(", Signature=")
$oAuthorization.Append($signature)
$oHttp.SetRequestHeader "Authorization",$oAuthorization.GetAsString()
$oUrl.SetString("https://")
$oUrl.Append($oHost.GetAsString())
$oUrl.Append($oUri.GetAsString())
If ($oQuery.Length > 0) Then
    $oUrl.Append("?")
    $oUrl.Append($oQuery.GetAsString())
EndIf

$bSuccess = $oHttp.HttpNoBody("HEAD",$oUrl.GetAsString(),$oResponse)
If (($bSuccess <> True) Or ($oResponse.StatusCode <> 200)) Then
    ConsoleWrite("Request failed or returned unexpected HTTP status: " & $oResponse.StatusCode & @CRLF)
    ;  Preserve progress. Inspect full diagnostics privately; do not log secrets.
    Exit
EndIf

$sEtag = $oResponse.GetHeaderField("ETag")
$oStringCompare.SetString($sEtag)
Local $bStringMatch5 = $oStringCompare.ContentsEqual("",True)
If (($oResponse.LastMethodSuccess <> True) Or ($bStringMatch5 = True)) Then
    ConsoleWrite("Missing required response header ETag." & @CRLF)
    Exit
EndIf

$sExpectedSize = $oResponse.GetHeaderField("Content-Length")
$oStringCompare.SetString($sExpectedSize)
Local $bStringMatch6 = $oStringCompare.ContentsEqual("",True)
If (($oResponse.LastMethodSuccess <> True) Or ($bStringMatch6 = True)) Then
    ConsoleWrite("Missing required response header Content-Length." & @CRLF)
    Exit
EndIf

$sReturnedVersion = $oResponse.GetHeaderField("x-amz-version-id")
If ($oResponse.LastMethodSuccess <> True) Then
    $sReturnedVersion = ""
EndIf

$oStringCompare.SetString($sVersionId)
Local $bStringMatch7 = $oStringCompare.ContentsEqual("",True)
If ($bStringMatch7 = True) Then
    $sVersionId = $sReturnedVersion
Else
    $oStringCompare.SetString($sReturnedVersion)
Local $bStringMatch8 = $oStringCompare.ContentsEqual($sVersionId,True)
    If ($bStringMatch8 <> True) Then
        ConsoleWrite("Unexpected object version." & @CRLF)
        Exit
    EndIf

EndIf

$sExpectedChecksum = $oResponse.GetHeaderField("x-amz-checksum-sha256")
$oStringCompare.SetString($sExpectedChecksum)
Local $bStringMatch9 = $oStringCompare.ContentsEqual("",True)
If (($oResponse.LastMethodSuccess <> True) Or ($bStringMatch9 = True)) Then
    ConsoleWrite("Missing required response header x-amz-checksum-sha256." & @CRLF)
    Exit
EndIf

$sChecksumType = $oResponse.GetHeaderField("x-amz-checksum-type")
$oStringCompare.SetString($sChecksumType)
Local $bStringMatch10 = $oStringCompare.ContentsEqual("",True)
If (($oResponse.LastMethodSuccess <> True) Or ($bStringMatch10 = True)) Then
    ConsoleWrite("Missing required response header x-amz-checksum-type." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($sChecksumType)
Local $bStringMatch11 = $oStringCompare.ContentsEqual("FULL_OBJECT",True)
If ($bStringMatch11 <> True) Then
    ConsoleWrite("This example requires a FULL_OBJECT SHA-256 checksum; COMPOSITE needs separate verification." & @CRLF)
    Exit
EndIf

$oQuery.Clear 
$oStringCompare.SetString($sVersionId)
Local $bStringMatch12 = $oStringCompare.ContentsEqual("",True)
If ($bStringMatch12 <> True) Then
    If ($oQuery.Length > 0) Then
        $oQuery.Append("&")
    EndIf

    $oQuery.Append("versionId=")
    $oQuery.Append($oHttp.UrlEncode($sVersionId))
EndIf

$oHttp.ClearHeaders 
$bSuccess = $oNow.SetFromCurrentSystemTime()
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read system time." & @CRLF)
    Exit
EndIf

$oTimestamp.SetString($oNow.GetAsTimestamp(False))
$iReplaceCount = $oTimestamp.Replace("-","")
$iReplaceCount = $oTimestamp.Replace(":","")
$sAmzDate = $oTimestamp.GetAsString()
$sDateStamp = $oTimestamp.GetRange(0,8,False)
$sPayloadHash = $oSigner.HashStringENC("")
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not hash payload." & @CRLF)
    Exit
EndIf

$oCanonicalHeaders.Clear 
$oSignedHeaders.Clear 
If ($oSignedHeaders.Length > 0) Then
    $oSignedHeaders.Append(";")
EndIf

$oSignedHeaders.Append("host")
$oCanonicalHeaders.Append("host:")
$oCanonicalHeaders.Append($oHost.GetAsString())
$oCanonicalHeaders.Append(@LF)
$oHttp.SetRequestHeader "host",$oHost.GetAsString()
If ($oSignedHeaders.Length > 0) Then
    $oSignedHeaders.Append(";")
EndIf

$oSignedHeaders.Append("if-match")
$oCanonicalHeaders.Append("if-match:")
$oCanonicalHeaders.Append($sEtag)
$oCanonicalHeaders.Append(@LF)
$oHttp.SetRequestHeader "if-match",$sEtag
If ($oSignedHeaders.Length > 0) Then
    $oSignedHeaders.Append(";")
EndIf

$oSignedHeaders.Append("x-amz-checksum-mode")
$oCanonicalHeaders.Append("x-amz-checksum-mode:")
$oCanonicalHeaders.Append("ENABLED")
$oCanonicalHeaders.Append(@LF)
$oHttp.SetRequestHeader "x-amz-checksum-mode","ENABLED"
If ($oSignedHeaders.Length > 0) Then
    $oSignedHeaders.Append(";")
EndIf

$oSignedHeaders.Append("x-amz-content-sha256")
$oCanonicalHeaders.Append("x-amz-content-sha256:")
$oCanonicalHeaders.Append($sPayloadHash)
$oCanonicalHeaders.Append(@LF)
$oHttp.SetRequestHeader "x-amz-content-sha256",$sPayloadHash
If ($oSignedHeaders.Length > 0) Then
    $oSignedHeaders.Append(";")
EndIf

$oSignedHeaders.Append("x-amz-date")
$oCanonicalHeaders.Append("x-amz-date:")
$oCanonicalHeaders.Append($sAmzDate)
$oCanonicalHeaders.Append(@LF)
$oHttp.SetRequestHeader "x-amz-date",$sAmzDate
If ($oCredentials.HasMember("session_token") = True) Then
    If ($oSignedHeaders.Length > 0) Then
        $oSignedHeaders.Append(";")
    EndIf

    $oSignedHeaders.Append("x-amz-security-token")
    $oCanonicalHeaders.Append("x-amz-security-token:")
    $oCanonicalHeaders.Append($oCredentials.StringOf("session_token"))
    $oCanonicalHeaders.Append(@LF)
    $oHttp.SetRequestHeader "x-amz-security-token",$oCredentials.StringOf("session_token")
EndIf

$oCanonical.Clear 
$oCanonical.Append("GET" & @LF)
$oCanonical.Append($oUri.GetAsString())
$oCanonical.Append(@LF)
$oCanonical.Append($oQuery.GetAsString())
$oCanonical.Append(@LF)
$oCanonical.Append($oCanonicalHeaders.GetAsString())
$oCanonical.Append(@LF)
$oCanonical.Append($oSignedHeaders.GetAsString())
$oCanonical.Append(@LF)
$oCanonical.Append($sPayloadHash)
$sCanonicalHash = $oSigner.HashStringENC($oCanonical.GetAsString())
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not hash canonical request." & @CRLF)
    Exit
EndIf

$oScope.Clear 
$oScope.Append($sDateStamp)
$oScope.Append("/")
$oScope.Append($sRegion)
$oScope.Append("/s3/aws4_request")
$oStringToSign.Clear 
$oStringToSign.Append("AWS4-HMAC-SHA256" & @LF)
$oStringToSign.Append($sAmzDate)
$oStringToSign.Append(@LF)
$oStringToSign.Append($oScope.GetAsString())
$oStringToSign.Append(@LF)
$oStringToSign.Append($sCanonicalHash)
$oSigningKey.SetString("AWS4")
$oSigningKey.Append($oCredentials.StringOf("secret_key"))
$bSuccess = $oSigner.SetMacKeyString($oSigningKey.GetAsString())
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set signing key." & @CRLF)
    Exit
EndIf

$sDerivedKey = $oSigner.MacStringENC($sDateStamp)
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not derive signing key." & @CRLF)
    Exit
EndIf

$bSuccess = $oSigner.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set signing key." & @CRLF)
    Exit
EndIf

$sDerivedKey = $oSigner.MacStringENC($sRegion)
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not derive signing key." & @CRLF)
    Exit
EndIf

$bSuccess = $oSigner.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set signing key." & @CRLF)
    Exit
EndIf

$sDerivedKey = $oSigner.MacStringENC("s3")
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not derive signing key." & @CRLF)
    Exit
EndIf

$bSuccess = $oSigner.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set signing key." & @CRLF)
    Exit
EndIf

$sDerivedKey = $oSigner.MacStringENC("aws4_request")
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not derive signing key." & @CRLF)
    Exit
EndIf

$bSuccess = $oSigner.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set signing key." & @CRLF)
    Exit
EndIf

$signature = $oSigner.MacStringENC($oStringToSign.GetAsString())
If ($oSigner.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not sign request." & @CRLF)
    Exit
EndIf

$oAuthorization.Clear 
$oAuthorization.Append("AWS4-HMAC-SHA256 Credential=")
$oAuthorization.Append($oCredentials.StringOf("access_key"))
$oAuthorization.Append("/")
$oAuthorization.Append($oScope.GetAsString())
$oAuthorization.Append(", SignedHeaders=")
$oAuthorization.Append($oSignedHeaders.GetAsString())
$oAuthorization.Append(", Signature=")
$oAuthorization.Append($signature)
$oHttp.SetRequestHeader "Authorization",$oAuthorization.GetAsString()
$oUrl.SetString("https://")
$oUrl.Append($oHost.GetAsString())
$oUrl.Append($oUri.GetAsString())
If ($oQuery.Length > 0) Then
    $oUrl.Append("?")
    $oUrl.Append($oQuery.GetAsString())
EndIf

$bSuccess = $oHttp.Download($oUrl.GetAsString(),$sPartialPath)
If (($bSuccess <> True) Or ($oHttp.LastStatus <> 200)) Then
    ConsoleWrite("Request failed or returned unexpected HTTP status: " & $oHttp.LastStatus & @CRLF)
    ;  Preserve progress. Inspect full diagnostics privately; do not log secrets.
    Exit
EndIf

$oReceived = ObjCreate("Chilkat.Mime")
$bSuccess = $oReceived.LoadMime($oHttp.LastResponseHeader)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not parse download headers." & @CRLF)
    Exit
EndIf

Local $sReceivedSize
Local $sReceivedHash
Local $sReceivedType
$sReceivedSize = $oReceived.GetHeaderField("Content-Length")
$oStringCompare.SetString($sReceivedSize)
Local $bStringMatch13 = $oStringCompare.ContentsEqual("",True)
If (($oReceived.LastMethodSuccess <> True) Or ($bStringMatch13 = True)) Then
    ConsoleWrite("Missing download header Content-Length." & @CRLF)
    Exit
EndIf

$sReceivedHash = $oReceived.GetHeaderField("x-amz-checksum-sha256")
$oStringCompare.SetString($sReceivedHash)
Local $bStringMatch14 = $oStringCompare.ContentsEqual("",True)
If (($oReceived.LastMethodSuccess <> True) Or ($bStringMatch14 = True)) Then
    ConsoleWrite("Missing download header x-amz-checksum-sha256." & @CRLF)
    Exit
EndIf

$sReceivedType = $oReceived.GetHeaderField("x-amz-checksum-type")
$oStringCompare.SetString($sReceivedType)
Local $bStringMatch15 = $oStringCompare.ContentsEqual("",True)
If (($oReceived.LastMethodSuccess <> True) Or ($bStringMatch15 = True)) Then
    ConsoleWrite("Missing download header x-amz-checksum-type." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($sReceivedSize)
Local $bStringMatch16 = $oStringCompare.ContentsEqual($sExpectedSize,True)
$oStringCompare.SetString($sReceivedHash)
Local $bStringMatch17 = $oStringCompare.ContentsEqual($sExpectedChecksum,True)
$oStringCompare.SetString($sReceivedType)
Local $bStringMatch18 = $oStringCompare.ContentsEqual("FULL_OBJECT",True)
If (($bStringMatch16 <> True) Or ($bStringMatch17 <> True) Or ($bStringMatch18 <> True)) Then
    ConsoleWrite("Download metadata changed; preserve the temporary file." & @CRLF)
    Exit
EndIf

Local $sActualSize = $oFiles.FileSizeStr($sPartialPath)
$oStringCompare.SetString($sActualSize)
Local $bStringMatch19 = $oStringCompare.ContentsEqual($sExpectedSize,True)
If (($oFiles.LastMethodSuccess <> True) Or ($bStringMatch19 <> True)) Then
    ConsoleWrite("Downloaded size mismatch." & @CRLF)
    Exit
EndIf

$oDigest = ObjCreate("Chilkat.Crypt2")
$oDigest.HashAlgorithm = "sha256"
$oDigest.EncodingMode = "base64"
Local $sActualChecksum = $oDigest.HashFileENC($sPartialPath)
$oStringCompare.SetString($sActualChecksum)
Local $bStringMatch20 = $oStringCompare.ContentsEqual($sExpectedChecksum,True)
If (($oDigest.LastMethodSuccess <> True) Or ($bStringMatch20 <> True)) Then
    ConsoleWrite("Downloaded checksum mismatch." & @CRLF)
    Exit
EndIf

$bSuccess = $oFiles.FileRename($sPartialPath,$sFinalPath)
If ($bSuccess <> True) Then
    ConsoleWrite("Verified download remains at the temporary path." & @CRLF)
    Exit
EndIf

ConsoleWrite("Full-object SHA-256 download verified." & @CRLF)