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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loTextCompare
LOCAL loCredentials
LOCAL lnSameText1
LOCAL lnSameText2
LOCAL lcRegion
LOCAL lcBucketName
LOCAL loEndpoint
LOCAL loAws
LOCAL loRest
LOCAL lnSameText3
LOCAL loConnection
LOCAL loXml
LOCAL loState
LOCAL lnSameText4
LOCAL lnSameText5
LOCAL lnSameText6
LOCAL lnSameText7
LOCAL lnSameText8
LOCAL lnSameText9
LOCAL lcUploadId
LOCAL lcObjectKey
LOCAL loObjectPath
LOCAL lnRestoredSlashes
LOCAL lcInputFile
LOCAL lnPartSize
LOCAL loFiles
LOCAL lnNumParts
LOCAL loParts
LOCAL loPartNumberText
LOCAL lnPartNumber
LOCAL loPart
LOCAL loPartResponse
LOCAL lcResponseBody
LOCAL lnSameText10
LOCAL lcEtag
LOCAL lnSameText11
lnSuccess = 0
* StringBuilder compares text by value in every generated language.
loTextCompare = CreateObject('Chilkat.StringBuilder')
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
* Private configuration, not a file in public web content or source control.
* Load current IAM credentials authorized for the S3 operation.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/s3.json")
IF (lnSuccess <> 1) THEN
? "Could not load private AWS credentials."
RELEASE loTextCompare
RELEASE loCredentials
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loCredentials.StringOf("access_key"))
lnSameText1 = loTextCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (lnSameText1 = 1)) THEN
? "Missing or invalid access_key."
RELEASE loTextCompare
RELEASE loCredentials
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loCredentials.StringOf("secret_key"))
lnSameText2 = loTextCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (lnSameText2 = 1)) THEN
? "Missing or invalid secret_key."
RELEASE loTextCompare
RELEASE loCredentials
CANCEL
ENDIF
* General purpose bucket in the commercial AWS partition.
lcRegion = "us-west-2"
lcBucketName = "amzn-s3-demo-bucket"
loEndpoint = CreateObject('Chilkat.StringBuilder')
lnSuccess = loEndpoint.Append(lcBucketName)
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loEndpoint.Append(".")
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loEndpoint.Append("s3.")
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loEndpoint.Append(lcRegion)
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loEndpoint.Append(".amazonaws.com")
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
CANCEL
ENDIF
loAws = CreateObject('Chilkat.AuthAws')
loAws.AccessKey = loCredentials.StringOf("access_key")
loAws.SecretKey = loCredentials.StringOf("secret_key")
loAws.Region = lcRegion
loAws.ServiceName = "s3"
loAws.SignatureVersion = 4
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
lnSuccess = loRest.SetAuthAws(loAws)
IF (lnSuccess <> 1) THEN
? "Could not configure AWS signing."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
* Temporary credentials require this token in every signed query.
* Keep request URLs private. Do not clear this parameter between requests.
IF (loCredentials.HasMember("session_token") = 1) THEN
IF (loCredentials.JsonTypeOf("session_token") <> 1) THEN
? "session_token must be a string."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loCredentials.StringOf("session_token"))
lnSameText3 = loTextCompare.ContentsEqual("",1)
IF (lnSameText3 <> 1) THEN
lnSuccess = loRest.AddQueryParam("X-Amz-Security-Token",loCredentials.StringOf("session_token"))
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
ENDIF
ENDIF
* Connect to the SAME hostname used for TLS verification and request signing.
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
? "TLS connection failed; inspect diagnostics privately."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
lnSuccess = loRest.UseConnection(loConnection,1)
IF (lnSuccess <> 1) THEN
? "Could not attach the TLS connection."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
loXml = CreateObject('Chilkat.Xml')
loXml.Strict = 1
loState = CreateObject('Chilkat.JsonObject')
loState.Strict = 1
lnSuccess = loState.LoadFile("s3-multipart.json")
IF (lnSuccess <> 1) THEN
? "Could not load multipart state."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loState.StringOf("bucket"))
lnSameText4 = loTextCompare.ContentsEqual("",1)
IF ((loState.JsonTypeOf("bucket") <> 1) OR (lnSameText4 = 1)) THEN
? "Invalid multipart state field: bucket"
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loState.StringOf("region"))
lnSameText5 = loTextCompare.ContentsEqual("",1)
IF ((loState.JsonTypeOf("region") <> 1) OR (lnSameText5 = 1)) THEN
? "Invalid multipart state field: region"
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loState.StringOf("key"))
lnSameText6 = loTextCompare.ContentsEqual("",1)
IF ((loState.JsonTypeOf("key") <> 1) OR (lnSameText6 = 1)) THEN
? "Invalid multipart state field: key"
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loState.StringOf("upload_id"))
lnSameText7 = loTextCompare.ContentsEqual("",1)
IF ((loState.JsonTypeOf("upload_id") <> 1) OR (lnSameText7 = 1)) THEN
? "Invalid multipart state field: upload_id"
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loState.StringOf("bucket"))
lnSameText8 = loTextCompare.ContentsEqual(lcBucketName,1)
loTextCompare.Clear()
loTextCompare.SetString(loState.StringOf("region"))
lnSameText9 = loTextCompare.ContentsEqual(lcRegion,1)
IF ((lnSameText8 <> 1) OR (lnSameText9 <> 1)) THEN
? "Multipart state belongs to another bucket or region."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
CANCEL
ENDIF
lcUploadId = loState.StringOf("upload_id")
* Supply an unencoded object key, not a URL. Preserve slashes in the key.
lcObjectKey = loState.StringOf("key")
loObjectPath = CreateObject('Chilkat.StringBuilder')
lnSuccess = loObjectPath.Append(lcObjectKey)
IF (lnSuccess <> 1) THEN
? "Could not build text."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
CANCEL
ENDIF
lnSuccess = loObjectPath.Encode("url_rfc3986","utf-8")
IF (lnSuccess <> 1) THEN
? "Could not encode the object key."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
CANCEL
ENDIF
lnRestoredSlashes = loObjectPath.Replace("%2F","/")
lnSuccess = loObjectPath.Prepend("/")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
CANCEL
ENDIF
* Keep this file unchanged throughout the upload workflow.
lcInputFile = "qa_data/files/example.bin"
lnPartSize = 8388608
loFiles = CreateObject('Chilkat.FileAccess')
lnSuccess = loFiles.OpenForRead(lcInputFile)
IF (lnSuccess <> 1) THEN
? "Could not open the input file."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
CANCEL
ENDIF
lnNumParts = loFiles.GetNumBlocks(lnPartSize)
loFiles.FileClose()
IF ((lnNumParts < 1) OR (lnNumParts > 10000)) THEN
? "Choose a larger part size for more than 10000 parts; use PutObject for an empty file."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
CANCEL
ENDIF
* Invalidate any prior completion record before changing uploaded parts.
loState.Delete("completion_xml")
loState.Delete("part_count")
lnSuccess = loState.WriteFile("s3-multipart.json")
IF (lnSuccess <> 1) THEN
? "Could not invalidate the prior completion record."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
CANCEL
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.
loParts = CreateObject('Chilkat.Xml')
loParts.Tag = "CompleteMultipartUpload"
lnSuccess = loParts.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("uploadId",lcUploadId)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
CANCEL
ENDIF
loPartNumberText = CreateObject('Chilkat.StringBuilder')
lnPartNumber = 1
DO WHILE (lnPartNumber <= lnNumParts)
loPartNumberText.Clear()
lnSuccess = loPartNumberText.AppendInt(lnPartNumber)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
CANCEL
ENDIF
IF (lnPartNumber > 1) THEN
lnSuccess = loRest.RemoveQueryParam("partNumber")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
CANCEL
ENDIF
ENDIF
lnSuccess = loRest.AddQueryParam("partNumber",loPartNumberText.GetAsString())
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
CANCEL
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.
loPart = CreateObject('Chilkat.BinData')
lnSuccess = loFiles.OpenForRead(lcInputFile)
IF (lnSuccess <> 1) THEN
? "Could not reopen the unchanged input file."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
RELEASE loPart
CANCEL
ENDIF
lnSuccess = loFiles.ReadBlockBd(lnPartNumber - 1,lnPartSize,loPart)
loFiles.FileClose()
IF ((lnSuccess <> 1) OR (loPart.NumBytes = 0)) THEN
? "Could not read the expected part."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
RELEASE loPart
CANCEL
ENDIF
loPartResponse = CreateObject('Chilkat.StringBuilder')
lnSuccess = loRest.FullRequestBd("PUT",loObjectPath.GetAsString(),loPart,loPartResponse)
IF (lnSuccess <> 1) THEN
? "Part upload did not complete; inspect diagnostics privately."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
RELEASE loPart
RELEASE loPartResponse
CANCEL
ENDIF
lcResponseBody = loPartResponse.GetAsString()
IF (loRest.ResponseStatusCode <> 200) THEN
? "S3 HTTP status: " + STR(loRest.ResponseStatusCode)
lnSuccess = loXml.LoadXml(lcResponseBody)
loTextCompare.Clear()
loTextCompare.SetString(loXml.Tag)
lnSameText10 = loTextCompare.ContentsEqual("Error",1)
IF ((lnSuccess = 1) AND (lnSameText10 = 1)) THEN
? "S3 error code: " + loXml.GetChildContent("Code")
ENDIF
* Keep signed headers, credentials, URLs, and full diagnostics private.
* A timeout may leave a write's outcome uncertain. Check before retrying.
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
RELEASE loPart
RELEASE loPartResponse
CANCEL
ENDIF
lcEtag = loRest.ResponseHdrByName("ETag")
loTextCompare.Clear()
loTextCompare.SetString(lcEtag)
lnSameText11 = loTextCompare.ContentsEqual("",1)
IF ((loRest.LastMethodSuccess <> 1) OR (lnSameText11 = 1)) THEN
? "Missing part ETag."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
RELEASE loPart
RELEASE loPartResponse
CANCEL
ENDIF
loParts.I = lnPartNumber - 1
loParts.UpdateChildContentInt("Part[i]|PartNumber",lnPartNumber)
loParts.UpdateChildContent("Part[i]|ETag",lcEtag)
? "Uploaded part: " + STR(lnPartNumber)
lnPartNumber = lnPartNumber + 1
ENDDO
* Save only after every part succeeded. Bind the completion body to this state.
lnSuccess = loState.UpdateString("completion_xml",loParts.GetXml())
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
RELEASE loPart
RELEASE loPartResponse
CANCEL
ENDIF
lnSuccess = loState.UpdateInt("part_count",lnNumParts)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
RELEASE loPart
RELEASE loPartResponse
CANCEL
ENDIF
lnSuccess = loState.WriteFile("s3-multipart.json")
IF (lnSuccess <> 1) THEN
? "Parts uploaded but state save failed; do not complete with stale state."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
RELEASE loPart
RELEASE loPartResponse
CANCEL
ENDIF
? "All parts uploaded and completion data saved."
RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loState
RELEASE loObjectPath
RELEASE loFiles
RELEASE loParts
RELEASE loPartNumberText
RELEASE loPart
RELEASE loPartResponse