S3 Http: Resume and Recover a Multipart Upload
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.
Uses a private checkpoint directory for one upload and an immutable source file. Run one process per checkpoint; never run upload/resume concurrently with abort. The fixed 8 MiB part size limits this demonstration to nonempty files of at most 10,000 parts (78.125 GiB). It does not impose that limit on S3 itself; choose larger valid part sizes for larger objects.
Uses SHA-256 COMPOSITE multipart checksums, validates part checksums, and verifies the returned composite checksum. ETags are retained for completion but never treated as MD5. Completion can return an error inside HTTP 200; the XML result is checked. Source identity is SHA-256, not just filename/length. The checkpoint contains no AWS credentials.
Requires s3:PutObject, s3:ListMultipartUploadParts for resume, and s3:GetObject for uncertain-completion recovery, plus relevant KMS permissions if bucket defaults use KMS. Incomplete parts incur storage charges until completed or aborted. Configure a suitable incomplete-multipart lifecycle rule as an operational safeguard. Timeout during initiation may leave an upload without a checkpoint; list uploads and investigate before initiating another.
Lists all uploaded parts and reuses only parts whose SHA-256 and byte count match the local source; mismatched parts are replaced. If ListParts returns NoSuchUpload after a recorded completion attempt, a checksum-enabled HEAD must match the unique upload token, source hash, composite checksum and size before recovery is reported. A missing upload alone is never treated as completion. If another write has replaced the current object, inspect version history manually.
Chilkat AutoIt Downloads
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("/")
$oFiles = ObjCreate("Chilkat.FileAccess")
Local $sFilePath = "qa_data/bin/archive.bin"
Local $sFileSize = $oFiles.FileSizeStr($sFilePath)
If ($oFiles.LastMethodSuccess <> True) Then
ConsoleWrite("Could not read source file size." & @CRLF)
Exit
EndIf
$oDigest = ObjCreate("Chilkat.Crypt2")
$oDigest.HashAlgorithm = "sha256"
$oDigest.EncodingMode = "hexlower"
Local $sourceHash = $oDigest.HashFileENC($sFilePath)
If ($oDigest.LastMethodSuccess <> True) Then
ConsoleWrite("Could not hash source file." & @CRLF)
Exit
EndIf
$oDigest.EncodingMode = "base64"
Local $sourceChecksum = $oDigest.HashFileENC($sFilePath)
If ($oDigest.LastMethodSuccess <> True) Then
ConsoleWrite("Could not compute checksum." & @CRLF)
Exit
EndIf
Local $statePath = "qa_output/s3-multipart/state.json"
Local $sPendingPath = "qa_output/s3-multipart/completion.json"
Local $sReceiptPath = "qa_output/s3-multipart/completed.json"
$oState = ObjCreate("Chilkat.JsonObject")
$oState.Strict = True
$oPending = ObjCreate("Chilkat.JsonObject")
$oPending.Strict = True
Local $sUploadId
Local $sUploadToken
Local $sCompositeChecksum
$bSuccess = $oFiles.OpenForRead($sFilePath)
If ($bSuccess <> True) Then
ConsoleWrite("Could not open immutable source file." & @CRLF)
Exit
EndIf
Local $iPartSize = 8388608
Local $iNumParts = $oFiles.GetNumBlocks($iPartSize)
If (($iNumParts < 1) Or ($iNumParts > 10000)) Then
ConsoleWrite("Use a nonempty source of at most 10,000 fixed 8 MiB parts." & @CRLF)
Exit
EndIf
$oRemoteEtags = ObjCreate("Chilkat.Hashtable")
$oRemoteSizes = ObjCreate("Chilkat.Hashtable")
$oRemoteChecksums = ObjCreate("Chilkat.Hashtable")
$oPartNumbers = ObjCreate("Chilkat.Hashtable")
$oPartNumberText = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oState.LoadFile($statePath)
If ($bSuccess <> True) Then
ConsoleWrite("Could not load multipart checkpoint." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oState.StringOf("schema"))
Local $bStringMatch4 = $oStringCompare.ContentsEqual("1",True)
If ($bStringMatch4 <> True) Then
ConsoleWrite("Checkpoint/source identity differs. Do not resume another file or destination." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oState.StringOf("bucket"))
Local $bStringMatch5 = $oStringCompare.ContentsEqual($sBucketName,True)
If ($bStringMatch5 <> True) Then
ConsoleWrite("Checkpoint/source identity differs. Do not resume another file or destination." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oState.StringOf("key"))
Local $bStringMatch6 = $oStringCompare.ContentsEqual($sObjectKey,True)
If ($bStringMatch6 <> True) Then
ConsoleWrite("Checkpoint/source identity differs. Do not resume another file or destination." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oState.StringOf("region"))
Local $bStringMatch7 = $oStringCompare.ContentsEqual($sRegion,True)
If ($bStringMatch7 <> True) Then
ConsoleWrite("Checkpoint/source identity differs. Do not resume another file or destination." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oState.StringOf("filePath"))
Local $bStringMatch8 = $oStringCompare.ContentsEqual($sFilePath,True)
If ($bStringMatch8 <> True) Then
ConsoleWrite("Checkpoint/source identity differs. Do not resume another file or destination." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oState.StringOf("fileSize"))
Local $bStringMatch9 = $oStringCompare.ContentsEqual($sFileSize,True)
If ($bStringMatch9 <> True) Then
ConsoleWrite("Checkpoint/source identity differs. Do not resume another file or destination." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oState.StringOf("sourceHash"))
Local $bStringMatch10 = $oStringCompare.ContentsEqual($sourceHash,True)
If ($bStringMatch10 <> True) Then
ConsoleWrite("Checkpoint/source identity differs. Do not resume another file or destination." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oState.StringOf("partSize"))
Local $bStringMatch11 = $oStringCompare.ContentsEqual("8388608",True)
If ($bStringMatch11 <> True) Then
ConsoleWrite("Checkpoint/source identity differs. Do not resume another file or destination." & @CRLF)
Exit
EndIf
$sUploadId = $oState.StringOf("uploadId")
$sUploadToken = $oState.StringOf("uploadToken")
$oStringCompare.SetString($sUploadId)
Local $bStringMatch12 = $oStringCompare.ContentsEqual("",True)
$oStringCompare.SetString($sUploadToken)
Local $bStringMatch13 = $oStringCompare.ContentsEqual("",True)
If (($bStringMatch12 = True) Or ($bStringMatch13 = True) Or ($oState.JsonTypeOf("uploadId") <> 1) Or ($oState.JsonTypeOf("uploadToken") <> 1)) Then
ConsoleWrite("Checkpoint has no upload identity." & @CRLF)
Exit
EndIf
If ($oFiles.FileExists($sReceiptPath) = True) Then
$bSuccess = $oPending.LoadFile($sReceiptPath)
If ($bSuccess <> True) Then
ConsoleWrite("Invalid completion receipt." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oPending.StringOf("uploadId"))
Local $bStringMatch14 = $oStringCompare.ContentsEqual($sUploadId,True)
$oStringCompare.SetString($oPending.StringOf("sourceHash"))
Local $bStringMatch15 = $oStringCompare.ContentsEqual($sourceHash,True)
If (($bStringMatch14 <> True) Or ($bStringMatch15 <> True)) Then
ConsoleWrite("Completion receipt belongs to another upload." & @CRLF)
Exit
EndIf
ConsoleWrite("Multipart upload already completed; receipt retained." & @CRLF)
Exit
EndIf
$oSeen = ObjCreate("Chilkat.Hashtable")
Local $bMore = True
Local $iPage = 0
Local $sMarker = ""
Local $sTruncated
Local $sItem
While ($bMore = True)
If ($iPage = 10000) Then
ConsoleWrite("Page bound reached; results are incomplete." & @CRLF)
Exit
EndIf
$iPage = $iPage + 1
$oQuery.Clear
If ($oQuery.Length > 0) Then
$oQuery.Append("&")
EndIf
$oQuery.Append("max-parts=")
$oQuery.Append($oHttp.UrlEncode("1000"))
$oStringCompare.SetString($sMarker)
Local $bStringMatch16 = $oStringCompare.ContentsEqual("",True)
If ($bStringMatch16 <> True) Then
If ($oQuery.Length > 0) Then
$oQuery.Append("&")
EndIf
$oQuery.Append("part-number-marker=")
$oQuery.Append($oHttp.UrlEncode($sMarker))
EndIf
If ($oQuery.Length > 0) Then
$oQuery.Append("&")
EndIf
$oQuery.Append("uploadId=")
$oQuery.Append($oHttp.UrlEncode($sUploadId))
$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-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.HttpNoBody("GET",$oUrl.GetAsString(),$oResponse)
If ($oResponse.StatusCode = 404) Then
$bSuccess = $oXml.LoadXml($oResponse.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Could not parse missing-upload response." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oXml.GetChildContent("Code"))
Local $bStringMatch17 = $oStringCompare.ContentsEqual("NoSuchUpload",True)
If ($bStringMatch17 <> True) Then
ConsoleWrite("404 was not NoSuchUpload; inspect target bucket/key." & @CRLF)
Exit
EndIf
$bSuccess = $oPending.LoadFile($sPendingPath)
If ($bSuccess <> True) Then
ConsoleWrite("Upload no longer exists. No valid pending completion record; inspect object state manually." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oPending.StringOf("uploadId"))
Local $bStringMatch18 = $oStringCompare.ContentsEqual($sUploadId,True)
If ($bStringMatch18 <> True) Then
ConsoleWrite("Pending completion does not match this upload." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oPending.StringOf("sourceHash"))
Local $bStringMatch19 = $oStringCompare.ContentsEqual($sourceHash,True)
If ($bStringMatch19 <> True) Then
ConsoleWrite("Pending completion does not match this upload." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oPending.StringOf("uploadToken"))
Local $bStringMatch20 = $oStringCompare.ContentsEqual($sUploadToken,True)
If ($bStringMatch20 <> True) Then
ConsoleWrite("Pending completion does not match this upload." & @CRLF)
Exit
EndIf
$sCompositeChecksum = $oPending.StringOf("checksum")
$oStringCompare.SetString($sCompositeChecksum)
Local $bStringMatch21 = $oStringCompare.ContentsEqual("",True)
If (($oPending.LastMethodSuccess <> True) Or ($bStringMatch21 = True)) Then
ConsoleWrite("Missing pending checksum." & @CRLF)
Exit
EndIf
$oQuery.Clear
$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
$oStringCompare.SetString($oResponse.GetHeaderField("x-amz-meta-upload-token"))
Local $bStringMatch22 = $oStringCompare.ContentsEqual($sUploadToken,True)
If ($bStringMatch22 <> True) Then
ConsoleWrite("Cannot confirm uncertain completion. Keep checkpoint and inspect versions/object state." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oResponse.GetHeaderField("x-amz-meta-source-sha256"))
Local $bStringMatch23 = $oStringCompare.ContentsEqual($sourceHash,True)
If ($bStringMatch23 <> True) Then
ConsoleWrite("Cannot confirm uncertain completion. Keep checkpoint and inspect versions/object state." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oResponse.GetHeaderField("x-amz-checksum-sha256"))
Local $bStringMatch24 = $oStringCompare.ContentsEqual($sCompositeChecksum,True)
If ($bStringMatch24 <> True) Then
ConsoleWrite("Cannot confirm uncertain completion. Keep checkpoint and inspect versions/object state." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oResponse.GetHeaderField("x-amz-checksum-type"))
Local $bStringMatch25 = $oStringCompare.ContentsEqual("COMPOSITE",True)
If ($bStringMatch25 <> True) Then
ConsoleWrite("Cannot confirm uncertain completion. Keep checkpoint and inspect versions/object state." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oResponse.GetHeaderField("Content-Length"))
Local $bStringMatch26 = $oStringCompare.ContentsEqual($sFileSize,True)
If ($bStringMatch26 <> True) Then
ConsoleWrite("Cannot confirm uncertain completion. Keep checkpoint and inspect versions/object state." & @CRLF)
Exit
EndIf
$bSuccess = $oState.UpdateString("compositeChecksum",$sCompositeChecksum)
If ($bSuccess <> True) Then
ConsoleWrite("Could not save compositeChecksum." & @CRLF)
Exit
EndIf
$bSuccess = $oState.WriteFile("qa_output/s3-multipart/completed.json.tmp")
If ($bSuccess <> True) Then
ConsoleWrite("Completion succeeded but receipt could not be saved; preserve the checkpoint." & @CRLF)
Exit
EndIf
$bSuccess = $oFiles.FileRename("qa_output/s3-multipart/completed.json.tmp",$sReceiptPath)
If ($bSuccess <> True) Then
ConsoleWrite("Completion receipt remains at its temporary path." & @CRLF)
Exit
EndIf
ConsoleWrite("Multipart upload completed and verified." & @CRLF)
Exit
EndIf
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
$bSuccess = $oXml.LoadXml($oResponse.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Invalid XML response." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oXml.Tag)
Local $bStringMatch27 = $oStringCompare.ContentsEqual("ListPartsResult",True)
If ($bStringMatch27 <> True) Then
ConsoleWrite("Unexpected XML result, possibly an embedded S3 error. Inspect privately." & @CRLF)
Exit
EndIf
Local $i = 0
While ($i < $oXml.NumChildrenHavingTag("Part"))
$oXml.I = $i
Local $iRemoteNumber = $oXml.GetChildIntValue("Part[i]|PartNumber")
If (($oXml.LastMethodSuccess <> True) Or ($iRemoteNumber < 1) Or ($iRemoteNumber > $iNumParts)) Then
ConsoleWrite("Unexpected remote part number." & @CRLF)
Exit
EndIf
$oPartNumberText.Clear
$oPartNumberText.AppendInt($iRemoteNumber)
If ($oPartNumbers.Contains($oPartNumberText.GetAsString()) = True) Then
ConsoleWrite("Duplicate remote part number." & @CRLF)
Exit
EndIf
$bSuccess = $oPartNumbers.AddStr($oPartNumberText.GetAsString(),"seen")
If ($bSuccess <> True) Then
ConsoleWrite("Could not track remote part." & @CRLF)
Exit
EndIf
$sItem = $oXml.GetChildContent("Part[i]|ETag")
$oStringCompare.SetString($sItem)
Local $bStringMatch28 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch28 = True)) Then
ConsoleWrite("Missing required Part[i]|ETag." & @CRLF)
Exit
EndIf
$bSuccess = $oRemoteEtags.AddStr($oPartNumberText.GetAsString(),$sItem)
If ($bSuccess <> True) Then
ConsoleWrite("Could not retain part ETag." & @CRLF)
Exit
EndIf
$sItem = $oXml.GetChildContent("Part[i]|Size")
$oStringCompare.SetString($sItem)
Local $bStringMatch29 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch29 = True)) Then
ConsoleWrite("Missing required Part[i]|Size." & @CRLF)
Exit
EndIf
$bSuccess = $oRemoteSizes.AddStr($oPartNumberText.GetAsString(),$sItem)
If ($bSuccess <> True) Then
ConsoleWrite("Could not retain part size." & @CRLF)
Exit
EndIf
$sItem = $oXml.GetChildContent("Part[i]|ChecksumSHA256")
If ($oXml.LastMethodSuccess = True) Then
$bSuccess = $oRemoteChecksums.AddStr($oPartNumberText.GetAsString(),$sItem)
If ($bSuccess <> True) Then
ConsoleWrite("Could not retain part checksum." & @CRLF)
Exit
EndIf
EndIf
$i = $i + 1
Wend
$sTruncated = $oXml.GetChildContent("IsTruncated")
$oStringCompare.SetString($sTruncated)
Local $bStringMatch30 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch30 = True)) Then
ConsoleWrite("Missing required IsTruncated." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($sTruncated)
Local $bStringMatch31 = $oStringCompare.ContentsEqual("true",True)
$oStringCompare.SetString($sTruncated)
Local $bStringMatch32 = $oStringCompare.ContentsEqual("false",True)
If (($bStringMatch31 <> True) And ($bStringMatch32 <> True)) Then
ConsoleWrite("Invalid IsTruncated; results are incomplete." & @CRLF)
Exit
EndIf
$bMore = False
$oStringCompare.SetString($sTruncated)
Local $bStringMatch33 = $oStringCompare.ContentsEqual("true",True)
If ($bStringMatch33 = True) Then
$sMarker = $oXml.GetChildContent("NextPartNumberMarker")
$oStringCompare.SetString($sMarker)
Local $bStringMatch34 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch34 = True)) Then
ConsoleWrite("Missing required NextPartNumberMarker." & @CRLF)
Exit
EndIf
If ($oSeen.Contains($sMarker) = True) Then
ConsoleWrite("Repeated pagination marker; results are incomplete." & @CRLF)
Exit
EndIf
$bSuccess = $oSeen.AddStr($sMarker,"seen")
If ($bSuccess <> True) Then
ConsoleWrite("Could not track marker." & @CRLF)
Exit
EndIf
$bMore = True
EndIf
Wend
$oPart = ObjCreate("Chilkat.BinData")
$oConcatenatedDigests = ObjCreate("Chilkat.BinData")
$oCompletion = ObjCreate("Chilkat.Xml")
$oCompletion.Tag = "CompleteMultipartUpload"
$oCompletion.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/")
Local $iPartIndex = 0
Local $sPartChecksum
Local $sPartEtag
Local $sPartHash
Local $sConfirmed
Local $sizeText
While ($iPartIndex < $iNumParts)
$oPart.Clear()
$bSuccess = $oFiles.ReadBlockBd($iPartIndex,$iPartSize,$oPart)
If ($bSuccess <> True) Then
ConsoleWrite("Could not read source part; preserve checkpoint." & @CRLF)
Exit
EndIf
If (($oPart.NumBytes = 0) Or (($iPartIndex < $iNumParts - 1) And ($oPart.NumBytes <> $iPartSize))) Then
ConsoleWrite("Source changed or part read was incomplete." & @CRLF)
Exit
EndIf
$sPartChecksum = $oDigest.HashBdENC($oPart)
If ($oDigest.LastMethodSuccess <> True) Then
ConsoleWrite("Could not checksum part." & @CRLF)
Exit
EndIf
$sPartHash = $oSigner.HashBdENC($oPart)
If ($oSigner.LastMethodSuccess <> True) Then
ConsoleWrite("Could not hash part." & @CRLF)
Exit
EndIf
$bSuccess = $oConcatenatedDigests.AppendEncoded($sPartChecksum,"base64")
If ($bSuccess <> True) Then
ConsoleWrite("Could not accumulate part digests." & @CRLF)
Exit
EndIf
$oPartNumberText.Clear
$oPartNumberText.AppendInt($iPartIndex + 1)
$oNumber.Clear
$oNumber.AppendInt($oPart.NumBytes)
$sizeText = $oNumber.GetAsString()
Local $sPartNumberKey = $oPartNumberText.GetAsString()
Local $bReuse = False
$sPartEtag = ""
If ($oRemoteChecksums.Contains($sPartNumberKey) = True) Then
$oStringCompare.SetString($oRemoteChecksums.LookupStr($sPartNumberKey))
Local $bStringMatch35 = $oStringCompare.ContentsEqual($sPartChecksum,True)
$oStringCompare.SetString($oRemoteSizes.LookupStr($sPartNumberKey))
Local $bStringMatch36 = $oStringCompare.ContentsEqual($sizeText,True)
If (($bStringMatch35 = True) And ($bStringMatch36 = True)) Then
$sPartEtag = $oRemoteEtags.LookupStr($sPartNumberKey)
$oStringCompare.SetString($sPartEtag)
Local $bStringMatch37 = $oStringCompare.ContentsEqual("",True)
If (($oRemoteEtags.LastMethodSuccess = True) And ($bStringMatch37 <> True)) Then
$bReuse = True
EndIf
EndIf
EndIf
If ($bReuse <> True) Then
$oQuery.Clear
If ($oQuery.Length > 0) Then
$oQuery.Append("&")
EndIf
$oQuery.Append("partNumber=")
$oQuery.Append($oHttp.UrlEncode($oPartNumberText.GetAsString()))
If ($oQuery.Length > 0) Then
$oQuery.Append("&")
EndIf
$oQuery.Append("uploadId=")
$oQuery.Append($oHttp.UrlEncode($sUploadId))
$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 = $sPartHash
$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-sha256")
$oCanonicalHeaders.Append("x-amz-checksum-sha256:")
$oCanonicalHeaders.Append($sPartChecksum)
$oCanonicalHeaders.Append(@LF)
$oHttp.SetRequestHeader "x-amz-checksum-sha256",$sPartChecksum
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("PUT" & @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.HttpBd("PUT",$oUrl.GetAsString(),$oPart,"application/octet-stream",$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
$sConfirmed = $oResponse.GetHeaderField("x-amz-checksum-sha256")
$oStringCompare.SetString($sConfirmed)
Local $bStringMatch38 = $oStringCompare.ContentsEqual("",True)
If (($oResponse.LastMethodSuccess <> True) Or ($bStringMatch38 = True)) Then
ConsoleWrite("Missing required response header x-amz-checksum-sha256." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($sConfirmed)
Local $bStringMatch39 = $oStringCompare.ContentsEqual($sPartChecksum,True)
If ($bStringMatch39 <> True) Then
ConsoleWrite("Part checksum mismatch; preserve checkpoint." & @CRLF)
Exit
EndIf
$sPartEtag = $oResponse.GetHeaderField("ETag")
$oStringCompare.SetString($sPartEtag)
Local $bStringMatch40 = $oStringCompare.ContentsEqual("",True)
If (($oResponse.LastMethodSuccess <> True) Or ($bStringMatch40 = True)) Then
ConsoleWrite("Missing required response header ETag." & @CRLF)
Exit
EndIf
Else
ConsoleWrite("Reusing verified remote part: " & $oPartNumberText.GetAsString() & @CRLF)
EndIf
$oCompletion.I = $iPartIndex
$oCompletion.UpdateChildContent "Part[i]|PartNumber",$oPartNumberText.GetAsString()
$oCompletion.UpdateChildContent "Part[i]|ETag",$sPartEtag
$oCompletion.UpdateChildContent "Part[i]|ChecksumSHA256",$sPartChecksum
$iPartIndex = $iPartIndex + 1
Wend
$oFiles.FileClose
; Recheck the entire immutable source before committing its parts.
Local $sFinalSourceHash = $oSigner.HashFileENC($sFilePath)
$oStringCompare.SetString($sFinalSourceHash)
Local $bStringMatch41 = $oStringCompare.ContentsEqual($sourceHash,True)
If (($oSigner.LastMethodSuccess <> True) Or ($bStringMatch41 <> True)) Then
ConsoleWrite("Source changed; completion was not sent." & @CRLF)
Exit
EndIf
$sCompositeChecksum = $oDigest.HashBdENC($oConcatenatedDigests)
If ($oDigest.LastMethodSuccess <> True) Then
ConsoleWrite("Could not compute composite checksum." & @CRLF)
Exit
EndIf
$oNumber.SetString($sCompositeChecksum)
$oNumber.Append("-")
$oNumber.AppendInt($iNumParts)
$sCompositeChecksum = $oNumber.GetAsString()
$bSuccess = $oPending.UpdateString("uploadId",$sUploadId)
If ($bSuccess <> True) Then
ConsoleWrite("Could not save uploadId." & @CRLF)
Exit
EndIf
$bSuccess = $oPending.UpdateString("sourceHash",$sourceHash)
If ($bSuccess <> True) Then
ConsoleWrite("Could not save sourceHash." & @CRLF)
Exit
EndIf
$bSuccess = $oPending.UpdateString("uploadToken",$sUploadToken)
If ($bSuccess <> True) Then
ConsoleWrite("Could not save uploadToken." & @CRLF)
Exit
EndIf
$bSuccess = $oPending.UpdateString("checksum",$sCompositeChecksum)
If ($bSuccess <> True) Then
ConsoleWrite("Could not save checksum." & @CRLF)
Exit
EndIf
$bSuccess = $oPending.WriteFile($sPendingPath)
If ($bSuccess <> True) Then
ConsoleWrite("Could not save completion recovery record." & @CRLF)
Exit
EndIf
Local $sRequestBody = $oCompletion.GetXml()
$oQuery.Clear
If ($oQuery.Length > 0) Then
$oQuery.Append("&")
EndIf
$oQuery.Append("uploadId=")
$oQuery.Append($oHttp.UrlEncode($sUploadId))
$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($sRequestBody)
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-type")
$oCanonicalHeaders.Append("x-amz-checksum-type:")
$oCanonicalHeaders.Append("COMPOSITE")
$oCanonicalHeaders.Append(@LF)
$oHttp.SetRequestHeader "x-amz-checksum-type","COMPOSITE"
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("POST" & @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.HttpStr("POST",$oUrl.GetAsString(),$sRequestBody,"utf-8","application/xml",$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
$bSuccess = $oXml.LoadXml($oResponse.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Invalid XML response." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oXml.Tag)
Local $bStringMatch42 = $oStringCompare.ContentsEqual("CompleteMultipartUploadResult",True)
If ($bStringMatch42 <> True) Then
ConsoleWrite("Unexpected XML result, possibly an embedded S3 error. Inspect privately." & @CRLF)
Exit
EndIf
$sConfirmed = $oXml.GetChildContent("ChecksumSHA256")
$oStringCompare.SetString($sConfirmed)
Local $bStringMatch43 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch43 = True)) Then
ConsoleWrite("Missing required ChecksumSHA256." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($sConfirmed)
Local $bStringMatch44 = $oStringCompare.ContentsEqual($sCompositeChecksum,True)
If ($bStringMatch44 <> True) Then
ConsoleWrite("Completion checksum differs; preserve checkpoint and inspect object." & @CRLF)
Exit
EndIf
$bSuccess = $oState.UpdateString("compositeChecksum",$sCompositeChecksum)
If ($bSuccess <> True) Then
ConsoleWrite("Could not save compositeChecksum." & @CRLF)
Exit
EndIf
$bSuccess = $oState.WriteFile("qa_output/s3-multipart/completed.json.tmp")
If ($bSuccess <> True) Then
ConsoleWrite("Completion succeeded but receipt could not be saved; preserve the checkpoint." & @CRLF)
Exit
EndIf
$bSuccess = $oFiles.FileRename("qa_output/s3-multipart/completed.json.tmp",$sReceiptPath)
If ($bSuccess <> True) Then
ConsoleWrite("Completion receipt remains at its temporary path." & @CRLF)
Exit
EndIf
ConsoleWrite("Multipart upload completed and verified." & @CRLF)