Amazon S3: Upload Multipart Parts and Record ETags
See more S3 - REST Examples
Upload an unchanged file in 8 MiB parts and record each exact ETag in ascending part-number order. Requires s3:PutObject. All parts except the last must be at least 5 MiB; use no more than 10,000 parts and choose a larger part size for larger files. This demonstration buffers at most one part for signing rather than the whole file.
Restarting uploads every part again for the same upload ID. It intentionally does not claim durable resumability from a stale local list. Do not run simultaneous writers or change the input file. Prior completion data is invalidated before upload, and a new completion body is saved only after all parts succeed. To implement selective resume, bind state to a file fingerprint and reconcile server parts.
Part of the initiate / upload parts / complete workflow. State in s3-multipart.json binds the upload ID to its bucket, region and key. Keep it private. Incomplete parts incur storage charges until completed or aborted; configure an AbortIncompleteMultipartUpload lifecycle rule as cleanup protection. These examples use the default checksum behavior without selecting a separate composite checksum algorithm. If you choose one at initiation, store each required part checksum and supply it at completion.
For general purpose S3 buckets, using regional HTTPS endpoints and AWS Signature Version 4. Directory buckets (S3 Express), access points, Outposts, and other AWS partitions need their own endpoint/authentication rules. Use a bucket name without dots for these virtual-hosted TLS examples; see the path-style example for dotted names.
Provide current IAM credentials in a private qa_data/aws/s3.json file with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This file is explicitly loaded; these examples do not discover profiles or refresh credentials. Keep it outside public content and source control. The session token is included in the signed request. TLS certificate verification is enabled on the Socket used by Rest.
Transport success and HTTP success are different. Keep full diagnostics private; signed requests and presigned URLs can contain credentials or grant access. For retryable reads use bounded backoff with jitter. Before retrying a write with an uncertain outcome, check the resource state. Keep keys, upload IDs, version IDs, ETags, and large sizes as strings.
Chilkat AutoIt Downloads
Local $bSuccess = False
; StringBuilder compares text by value in every generated language.
$oTextCompare = ObjCreate("Chilkat.StringBuilder")
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
; Private configuration, not a file in public web content or source control.
; Load current IAM credentials authorized for the S3 operation.
$oCredentials = ObjCreate("Chilkat.JsonObject")
$oCredentials.Strict = True
$bSuccess = $oCredentials.LoadFile("qa_data/aws/s3.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load private AWS credentials." & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oCredentials.StringOf("access_key"))
Local $bSameText1 = $oTextCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("access_key") <> 1) Or ($bSameText1 = True)) Then
ConsoleWrite("Missing or invalid access_key." & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oCredentials.StringOf("secret_key"))
Local $bSameText2 = $oTextCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("secret_key") <> 1) Or ($bSameText2 = True)) Then
ConsoleWrite("Missing or invalid secret_key." & @CRLF)
Exit
EndIf
; General purpose bucket in the commercial AWS partition.
Local $sRegion = "us-west-2"
Local $sBucketName = "amzn-s3-demo-bucket"
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oEndpoint.Append($sBucketName)
If ($bSuccess <> True) Then
ConsoleWrite("Could not build text." & @CRLF)
Exit
EndIf
$bSuccess = $oEndpoint.Append(".")
If ($bSuccess <> True) Then
ConsoleWrite("Could not build text." & @CRLF)
Exit
EndIf
$bSuccess = $oEndpoint.Append("s3.")
If ($bSuccess <> True) Then
ConsoleWrite("Could not build text." & @CRLF)
Exit
EndIf
$bSuccess = $oEndpoint.Append($sRegion)
If ($bSuccess <> True) Then
ConsoleWrite("Could not build text." & @CRLF)
Exit
EndIf
$bSuccess = $oEndpoint.Append(".amazonaws.com")
If ($bSuccess <> True) Then
ConsoleWrite("Could not build text." & @CRLF)
Exit
EndIf
$oAws = ObjCreate("Chilkat.AuthAws")
$oAws.AccessKey = $oCredentials.StringOf("access_key")
$oAws.SecretKey = $oCredentials.StringOf("secret_key")
$oAws.Region = $sRegion
$oAws.ServiceName = "s3"
$oAws.SignatureVersion = 4
$oRest = ObjCreate("Chilkat.Rest")
$oRest.ConnectTimeoutMs = 20000
$oRest.IdleTimeoutMs = 60000
$bSuccess = $oRest.SetAuthAws($oAws)
If ($bSuccess <> True) Then
ConsoleWrite("Could not configure AWS signing." & @CRLF)
Exit
EndIf
; Temporary credentials require this token in every signed query.
; Keep request URLs private. Do not clear this parameter between requests.
If ($oCredentials.HasMember("session_token") = True) Then
If ($oCredentials.JsonTypeOf("session_token") <> 1) Then
ConsoleWrite("session_token must be a string." & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oCredentials.StringOf("session_token"))
Local $bSameText3 = $oTextCompare.ContentsEqual("",True)
If ($bSameText3 <> True) Then
$bSuccess = $oRest.AddQueryParam("X-Amz-Security-Token",$oCredentials.StringOf("session_token"))
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
EndIf
EndIf
; Connect to the SAME hostname used for TLS verification and request signing.
$oConnection = ObjCreate("Chilkat.Socket")
$oConnection.RequireSslCertVerify = True
$oConnection.MaxReadIdleMs = 60000
$oConnection.MaxSendIdleMs = 60000
$bSuccess = $oConnection.Connect($oEndpoint.GetAsString(),443,True,20000)
If ($bSuccess <> True) Then
ConsoleWrite("TLS connection failed; inspect diagnostics privately." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.UseConnection($oConnection,True)
If ($bSuccess <> True) Then
ConsoleWrite("Could not attach the TLS connection." & @CRLF)
Exit
EndIf
$oXml = ObjCreate("Chilkat.Xml")
$oXml.Strict = True
$oState = ObjCreate("Chilkat.JsonObject")
$oState.Strict = True
$bSuccess = $oState.LoadFile("s3-multipart.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load multipart state." & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oState.StringOf("bucket"))
Local $bSameText4 = $oTextCompare.ContentsEqual("",True)
If (($oState.JsonTypeOf("bucket") <> 1) Or ($bSameText4 = True)) Then
ConsoleWrite("Invalid multipart state field: bucket" & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oState.StringOf("region"))
Local $bSameText5 = $oTextCompare.ContentsEqual("",True)
If (($oState.JsonTypeOf("region") <> 1) Or ($bSameText5 = True)) Then
ConsoleWrite("Invalid multipart state field: region" & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oState.StringOf("key"))
Local $bSameText6 = $oTextCompare.ContentsEqual("",True)
If (($oState.JsonTypeOf("key") <> 1) Or ($bSameText6 = True)) Then
ConsoleWrite("Invalid multipart state field: key" & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oState.StringOf("upload_id"))
Local $bSameText7 = $oTextCompare.ContentsEqual("",True)
If (($oState.JsonTypeOf("upload_id") <> 1) Or ($bSameText7 = True)) Then
ConsoleWrite("Invalid multipart state field: upload_id" & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oState.StringOf("bucket"))
Local $bSameText8 = $oTextCompare.ContentsEqual($sBucketName,True)
$oTextCompare.Clear
$oTextCompare.SetString($oState.StringOf("region"))
Local $bSameText9 = $oTextCompare.ContentsEqual($sRegion,True)
If (($bSameText8 <> True) Or ($bSameText9 <> True)) Then
ConsoleWrite("Multipart state belongs to another bucket or region." & @CRLF)
Exit
EndIf
Local $sUploadId = $oState.StringOf("upload_id")
; Supply an unencoded object key, not a URL. Preserve slashes in the key.
Local $sObjectKey = $oState.StringOf("key")
$oObjectPath = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oObjectPath.Append($sObjectKey)
If ($bSuccess <> True) Then
ConsoleWrite("Could not build text." & @CRLF)
Exit
EndIf
$bSuccess = $oObjectPath.Encode("url_rfc3986","utf-8")
If ($bSuccess <> True) Then
ConsoleWrite("Could not encode the object key." & @CRLF)
Exit
EndIf
Local $iRestoredSlashes = $oObjectPath.Replace("%2F","/")
$bSuccess = $oObjectPath.Prepend("/")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
; Keep this file unchanged throughout the upload workflow.
Local $sInputFile = "qa_data/files/example.bin"
Local $iPartSize = 8388608
$oFiles = ObjCreate("Chilkat.FileAccess")
$bSuccess = $oFiles.OpenForRead($sInputFile)
If ($bSuccess <> True) Then
ConsoleWrite("Could not open the input file." & @CRLF)
Exit
EndIf
Local $iNumParts = $oFiles.GetNumBlocks($iPartSize)
$oFiles.FileClose
If (($iNumParts < 1) Or ($iNumParts > 10000)) Then
ConsoleWrite("Choose a larger part size for more than 10000 parts; use PutObject for an empty file." & @CRLF)
Exit
EndIf
; Invalidate any prior completion record before changing uploaded parts.
$oState.Delete("completion_xml")
$oState.Delete("part_count")
$bSuccess = $oState.WriteFile("s3-multipart.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not invalidate the prior completion record." & @CRLF)
Exit
EndIf
; This demonstration uploads EVERY part again if restarted. It never skips a
; part using an unbound/stale partsList.xml. Re-uploading a part replaces it.
$oParts = ObjCreate("Chilkat.Xml")
$oParts.Tag = "CompleteMultipartUpload"
$bSuccess = $oParts.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.AddQueryParam("uploadId",$sUploadId)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$oPartNumberText = ObjCreate("Chilkat.StringBuilder")
Local $iPartNumber = 1
While ($iPartNumber <= $iNumParts)
$oPartNumberText.Clear
$bSuccess = $oPartNumberText.AppendInt($iPartNumber)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
If ($iPartNumber > 1) Then
$bSuccess = $oRest.RemoveQueryParam("partNumber")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
EndIf
$bSuccess = $oRest.AddQueryParam("partNumber",$oPartNumberText.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
; Buffer only this 8 MiB part. Rest authenticates UploadPart with SigV4;
; this library uses S3's supported UNSIGNED-PAYLOAD mode for this operation.
; HTTPS protects the transfer; an explicit checksum is a separate option.
$oPart = ObjCreate("Chilkat.BinData")
$bSuccess = $oFiles.OpenForRead($sInputFile)
If ($bSuccess <> True) Then
ConsoleWrite("Could not reopen the unchanged input file." & @CRLF)
Exit
EndIf
$bSuccess = $oFiles.ReadBlockBd($iPartNumber - 1,$iPartSize,$oPart)
$oFiles.FileClose
If (($bSuccess <> True) Or ($oPart.NumBytes = 0)) Then
ConsoleWrite("Could not read the expected part." & @CRLF)
Exit
EndIf
$oPartResponse = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oRest.FullRequestBd("PUT",$oObjectPath.GetAsString(),$oPart,$oPartResponse)
If ($bSuccess <> True) Then
ConsoleWrite("Part upload did not complete; inspect diagnostics privately." & @CRLF)
Exit
EndIf
Local $sResponseBody = $oPartResponse.GetAsString()
If ($oRest.ResponseStatusCode <> 200) Then
ConsoleWrite("S3 HTTP status: " & $oRest.ResponseStatusCode & @CRLF)
$bSuccess = $oXml.LoadXml($sResponseBody)
$oTextCompare.Clear
$oTextCompare.SetString($oXml.Tag)
Local $bSameText10 = $oTextCompare.ContentsEqual("Error",True)
If (($bSuccess = True) And ($bSameText10 = True)) Then
ConsoleWrite("S3 error code: " & $oXml.GetChildContent("Code") & @CRLF)
EndIf
; Keep signed headers, credentials, URLs, and full diagnostics private.
; A timeout may leave a write's outcome uncertain. Check before retrying.
Exit
EndIf
Local $sEtag = $oRest.ResponseHdrByName("ETag")
$oTextCompare.Clear
$oTextCompare.SetString($sEtag)
Local $bSameText11 = $oTextCompare.ContentsEqual("",True)
If (($oRest.LastMethodSuccess <> True) Or ($bSameText11 = True)) Then
ConsoleWrite("Missing part ETag." & @CRLF)
Exit
EndIf
$oParts.I = $iPartNumber - 1
$oParts.UpdateChildContentInt "Part[i]|PartNumber",$iPartNumber
$oParts.UpdateChildContent "Part[i]|ETag",$sEtag
ConsoleWrite("Uploaded part: " & $iPartNumber & @CRLF)
$iPartNumber = $iPartNumber + 1
Wend
; Save only after every part succeeded. Bind the completion body to this state.
$bSuccess = $oState.UpdateString("completion_xml",$oParts.GetXml())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oState.UpdateInt("part_count",$iNumParts)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oState.WriteFile("s3-multipart.json")
If ($bSuccess <> True) Then
ConsoleWrite("Parts uploaded but state save failed; do not complete with stale state." & @CRLF)
Exit
EndIf
ConsoleWrite("All parts uploaded and completion data saved." & @CRLF)