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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoTextCompare
Handle hoCredentials
Boolean iSameText1
Boolean iSameText2
String sRegion
String sBucketName
Handle hoEndpoint
Variant vAws
Handle hoAws
Handle hoRest
Boolean iSameText3
Variant vConnection
Handle hoConnection
Handle hoXml
Handle hoState
Boolean iSameText4
Boolean iSameText5
Boolean iSameText6
Boolean iSameText7
Boolean iSameText8
Boolean iSameText9
String sUploadId
String sObjectKey
Handle hoObjectPath
Integer iRestoredSlashes
String sInputFile
Integer iPartSize
Handle hoFiles
Integer iNumParts
Handle hoParts
Variant vPart
NumberText Handle hoPartNumberText
Integer iPartNumber
Variant vPart
Handle hoPart
Variant vPart
Response Handle hoPartResponse
String sResponseBody
Boolean iSameText10
String sEtag
Boolean iSameText11
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// StringBuilder compares text by value in every generated language.
Get Create (RefClass(cComChilkatStringBuilder)) To hoTextCompare
If (Not(IsComObjectCreated(hoTextCompare))) Begin
Send CreateComObject of hoTextCompare
End
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Move False To iSuccess
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the S3 operation.
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 AWS credentials."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText1
Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
If ((iTemp1 <> 1) Or (iSameText1 = True)) Begin
Showln "Missing or invalid access_key."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText2
Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
If ((iTemp1 <> 1) Or (iSameText2 = True)) Begin
Showln "Missing or invalid secret_key."
Procedure_Return
End
// General purpose bucket in the commercial AWS partition.
Move "us-west-2" To sRegion
Move "amzn-s3-demo-bucket" To sBucketName
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint sBucketName To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get ComAppend Of hoEndpoint "." To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get ComAppend Of hoEndpoint "s3." To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get ComAppend Of hoEndpoint sRegion To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get Create (RefClass(cComChilkatAuthAws)) To hoAws
If (Not(IsComObjectCreated(hoAws))) Begin
Send CreateComObject of hoAws
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Set ComAccessKey Of hoAws To sTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Set ComSecretKey Of hoAws To sTemp1
Set ComRegion Of hoAws To sRegion
Set ComServiceName Of hoAws To "s3"
Set ComSignatureVersion Of hoAws To 4
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Set ComConnectTimeoutMs Of hoRest To 20000
Set ComIdleTimeoutMs Of hoRest To 60000
Get pvComObject of hoAws to vAws
Get ComSetAuthAws Of hoRest vAws To iSuccess
If (iSuccess <> True) Begin
Showln "Could not configure AWS signing."
Procedure_Return
End
// Temporary credentials require this token in every signed query.
// Keep request URLs private. Do not clear this parameter between requests.
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
If (iTemp1 <> 1) Begin
Showln "session_token must be a string."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText3
If (iSameText3 <> True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
End
End
// Connect to the SAME hostname used for TLS verification and request signing.
Get Create (RefClass(cComChilkatSocket)) To hoConnection
If (Not(IsComObjectCreated(hoConnection))) Begin
Send CreateComObject of hoConnection
End
Set ComRequireSslCertVerify Of hoConnection To True
Set ComMaxReadIdleMs Of hoConnection To 60000
Set ComMaxSendIdleMs Of hoConnection To 60000
Get ComGetAsString Of hoEndpoint To sTemp1
Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
If (iSuccess <> True) Begin
Showln "TLS connection failed; inspect diagnostics privately."
Procedure_Return
End
Get pvComObject of hoConnection to vConnection
Get ComUseConnection Of hoRest vConnection True To iSuccess
If (iSuccess <> True) Begin
Showln "Could not attach the TLS connection."
Procedure_Return
End
Get Create (RefClass(cComChilkatXml)) To hoXml
If (Not(IsComObjectCreated(hoXml))) Begin
Send CreateComObject of hoXml
End
Set ComStrict Of hoXml To True
Get Create (RefClass(cComChilkatJsonObject)) To hoState
If (Not(IsComObjectCreated(hoState))) Begin
Send CreateComObject of hoState
End
Set ComStrict Of hoState To True
Get ComLoadFile Of hoState "s3-multipart.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load multipart state."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoState "bucket" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText4
Get ComJsonTypeOf Of hoState "bucket" To iTemp1
If ((iTemp1 <> 1) Or (iSameText4 = True)) Begin
Showln "Invalid multipart state field: bucket"
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoState "region" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText5
Get ComJsonTypeOf Of hoState "region" To iTemp1
If ((iTemp1 <> 1) Or (iSameText5 = True)) Begin
Showln "Invalid multipart state field: region"
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoState "key" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText6
Get ComJsonTypeOf Of hoState "key" To iTemp1
If ((iTemp1 <> 1) Or (iSameText6 = True)) Begin
Showln "Invalid multipart state field: key"
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoState "upload_id" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText7
Get ComJsonTypeOf Of hoState "upload_id" To iTemp1
If ((iTemp1 <> 1) Or (iSameText7 = True)) Begin
Showln "Invalid multipart state field: upload_id"
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoState "bucket" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare sBucketName True To iSameText8
Send ComClear To hoTextCompare
Get ComStringOf Of hoState "region" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare sRegion True To iSameText9
If ((iSameText8 <> True) Or (iSameText9 <> True)) Begin
Showln "Multipart state belongs to another bucket or region."
Procedure_Return
End
Get ComStringOf Of hoState "upload_id" To sUploadId
// Supply an unencoded object key, not a URL. Preserve slashes in the key.
Get ComStringOf Of hoState "key" To sObjectKey
Get Create (RefClass(cComChilkatStringBuilder)) To hoObjectPath
If (Not(IsComObjectCreated(hoObjectPath))) Begin
Send CreateComObject of hoObjectPath
End
Get ComAppend Of hoObjectPath sObjectKey To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build text."
Procedure_Return
End
Get ComEncode Of hoObjectPath "url_rfc3986" "utf-8" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not encode the object key."
Procedure_Return
End
Get ComReplace Of hoObjectPath "%2F" "/" To iRestoredSlashes
Get ComPrepend Of hoObjectPath "/" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
// Keep this file unchanged throughout the upload workflow.
Move "qa_data/files/example.bin" To sInputFile
Move 8388608 To iPartSize
Get Create (RefClass(cComCkFileAccess)) To hoFiles
If (Not(IsComObjectCreated(hoFiles))) Begin
Send CreateComObject of hoFiles
End
Get ComOpenForRead Of hoFiles sInputFile To iSuccess
If (iSuccess <> True) Begin
Showln "Could not open the input file."
Procedure_Return
End
Get ComGetNumBlocks Of hoFiles iPartSize To iNumParts
Send ComFileClose To hoFiles
If ((iNumParts < 1) Or (iNumParts > 10000)) Begin
Showln "Choose a larger part size for more than 10000 parts; use PutObject for an empty file."
Procedure_Return
End
// Invalidate any prior completion record before changing uploaded parts.
Get ComDelete Of hoState "completion_xml" To iSuccess
Get ComDelete Of hoState "part_count" To iSuccess
Get ComWriteFile Of hoState "s3-multipart.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not invalidate the prior completion record."
Procedure_Return
End
// 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.
Get Create (RefClass(cComChilkatXml)) To hoParts
If (Not(IsComObjectCreated(hoParts))) Begin
Send CreateComObject of hoParts
End
Set ComTag Of hoParts To "CompleteMultipartUpload"
Get ComAddAttribute Of hoParts "xmlns" "http://s3.amazonaws.com/doc/2006-03-01/" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComAddQueryParam Of hoRest "uploadId" sUploadId To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoPartNumberText
If (Not(IsComObjectCreated(hoPartNumberText))) Begin
Send CreateComObject of hoPartNumberText
End
Move 1 To iPartNumber
While (iPartNumber <= iNumParts)
Send ComClear To hoPartNumberText
Get ComAppendInt Of hoPartNumberText iPartNumber To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
If (iPartNumber > 1) Begin
Get ComRemoveQueryParam Of hoRest "partNumber" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
End
Get ComGetAsString Of hoPartNumberText To sTemp1
Get ComAddQueryParam Of hoRest "partNumber" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
// 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.
Get Create (RefClass(cComChilkatBinData)) To hoPart
If (Not(IsComObjectCreated(hoPart))) Begin
Send CreateComObject of hoPart
End
Get ComOpenForRead Of hoFiles sInputFile To iSuccess
If (iSuccess <> True) Begin
Showln "Could not reopen the unchanged input file."
Procedure_Return
End
Get pvComObject of hoPart to vPart
Get ComReadBlockBd Of hoFiles (iPartNumber - 1) iPartSize vPart To iSuccess
Send ComFileClose To hoFiles
Get ComNumBytes Of hoPart To iTemp1
If ((iSuccess <> True) Or (iTemp1 = 0)) Begin
Showln "Could not read the expected part."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoPartResponse
If (Not(IsComObjectCreated(hoPartResponse))) Begin
Send CreateComObject of hoPartResponse
End
Get ComGetAsString Of hoObjectPath To sTemp1
Get pvComObject of hoPart to vPart
Get pvComObject of hoPartResponse to vPartResponse
Get ComFullRequestBd Of hoRest "PUT" sTemp1 vPart vPartResponse To iSuccess
If (iSuccess <> True) Begin
Showln "Part upload did not complete; inspect diagnostics privately."
Procedure_Return
End
Get ComGetAsString Of hoPartResponse To sResponseBody
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "S3 HTTP status: " iTemp1
Get ComLoadXml Of hoXml sResponseBody To iSuccess
Send ComClear To hoTextCompare
Get ComTag Of hoXml To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "Error" True To iSameText10
If ((iSuccess = True) And (iSameText10 = True)) Begin
Get ComGetChildContent Of hoXml "Code" To sTemp1
Showln "S3 error code: " sTemp1
End
// Keep signed headers, credentials, URLs, and full diagnostics private.
// A timeout may leave a write's outcome uncertain. Check before retrying.
Procedure_Return
End
Get ComResponseHdrByName Of hoRest "ETag" To sEtag
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sEtag To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText11
Get ComLastMethodSuccess Of hoRest To bTemp1
If ((bTemp1 <> True) Or (iSameText11 = True)) Begin
Showln "Missing part ETag."
Procedure_Return
End
Set ComI Of hoParts To iPartNumber - 1
Send ComUpdateChildContentInt To hoParts "Part[i]|PartNumber" iPartNumber
Send ComUpdateChildContent To hoParts "Part[i]|ETag" sEtag
Showln "Uploaded part: " iPartNumber
Move (iPartNumber + 1) To iPartNumber
Loop
// Save only after every part succeeded. Bind the completion body to this state.
Get ComGetXml Of hoParts To sTemp1
Get ComUpdateString Of hoState "completion_xml" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComUpdateInt Of hoState "part_count" iNumParts To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComWriteFile Of hoState "s3-multipart.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Parts uploaded but state save failed; do not complete with stale state."
Procedure_Return
End
Showln "All parts uploaded and completion data saved."
End_Procedure