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

Amazon S3: Copy an Object Using Multipart Copy

See more S3 - REST Examples

Copy an object inside a general purpose bucket without downloading it. Initiate a destination upload, issue UploadPartCopy ranges, and complete it with the exact returned ETags. Requires source read and destination write permissions, plus applicable KMS permissions. HTTP 200 copy/completion bodies are checked for embedded errors.

The example uses 1,000,000,000-byte parts and supports objects larger than 5 MiB through 10,000,000,000,000 bytes (10 TB), with up to 10,000 parts. Decimal-string offset construction avoids int64 and 32-bit overflow. This is an example limit, not S3’s maximum object size; larger objects require larger parts. Only the final part may be below 5 MiB.

A source version is pinned when returned by HEAD, and every part uses the original ETag condition. Choose a distinct destination key; completing can replace that key or create a new version. Metadata and tags are not automatically inherited by multipart initiation; set the desired destination metadata, content type and encryption there. The source remains unchanged.

The saved s3-multipart.json can be used with the existing abort example after a failure. Completion XML is saved only after all copy parts succeed. Do not restart into the same state file blindly; an uncertain initiation/completion needs inspection. This example does not implement selective resume, and incomplete parts incur charges until completed or aborted.

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.

AWS UploadPartCopy reference

Chilkat DataFlex Downloads

DataFlex
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
    String sObjectKey
    Handle hoObjectPath
    Integer iRestoredSlashes
    Integer iStatusCode
    String sSourceEtag
    Boolean iSameText4
    String sSourceSize
    Boolean iSameText5
    String sSourceVersion
    Handle hoSizeText
    Handle hoDigits
    Handle hoPiece
    Integer iPosition
    String sDigit
    Integer iFullBillions
    Integer iTailBytes
    Integer iNumParts
    Handle hoFiles
    Handle hoCopySource
    Boolean iSameText6
    Handle hoEncodedVersion
    String sDestinationKey
    Boolean iSameText7
    Integer iDestinationSlashes
    String sResponseBody
    Boolean iSameText8
    Boolean iSameText9
    Boolean iSameText10
    String sUploadId
    Handle hoState
    Handle hoParts
    Handle hoRange
    Handle hoEnding
    Handle hoNumber
    Integer iPartNumber
    Boolean iSameText11
    Boolean iSameText12
    Boolean iSameText13
    Boolean iSameText14
    Boolean iSameText15
    Boolean iSameText16
    String sTemp1
    String sTemp2
    Integer iTemp1
    Integer iTemp2
    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
    //  Supply an unencoded object key, not a URL. Preserve slashes in the key.
    Move "documents/source.bin" 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

    Get ComGetAsString Of hoObjectPath To sTemp1
    Get ComSendReqNoBody Of hoRest "HEAD" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not send HEAD request."
        Procedure_Return
    End

    Get ComReadResponseHeader Of hoRest To iStatusCode
    If (iStatusCode < 0) Begin
        Showln "Could not read the response header."
        Procedure_Return
    End

    If (iStatusCode <> 200) Begin
        Showln "HEAD did not return HTTP 200. A 403 is not proof that an object is missing."
        Procedure_Return
    End

    //  HEAD has no response body; do not call a body-reading method.
    Get ComResponseHdrByName Of hoRest "ETag" To sSourceEtag
    Send ComClear To hoTextCompare
    Get ComSetString Of hoTextCompare sSourceEtag To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText4
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If ((bTemp1 <> True) Or (iSameText4 = True)) Begin
        Showln "Missing response header: ETag"
        Procedure_Return
    End

    Get ComResponseHdrByName Of hoRest "Content-Length" To sSourceSize
    Send ComClear To hoTextCompare
    Get ComSetString Of hoTextCompare sSourceSize To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText5
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If ((bTemp1 <> True) Or (iSameText5 = True)) Begin
        Showln "Missing response header: Content-Length"
        Procedure_Return
    End

    Get ComResponseHdrByName Of hoRest "x-amz-version-id" To sSourceVersion
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Move "" To sSourceVersion
    End

    //  Split a decimal size into billions and a remainder. No large integer type.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSizeText
    If (Not(IsComObjectCreated(hoSizeText))) Begin
        Send CreateComObject of hoSizeText
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoDigits
    If (Not(IsComObjectCreated(hoDigits))) Begin
        Send CreateComObject of hoDigits
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoPiece
    If (Not(IsComObjectCreated(hoPiece))) Begin
        Send CreateComObject of hoPiece
    End
    Get ComAppend Of hoSizeText sSourceSize To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build text."
        Procedure_Return
    End

    Get ComAppend Of hoDigits "0123456789" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build text."
        Procedure_Return
    End

    Get ComLength Of hoSizeText To iTemp1
    Get ComLength Of hoSizeText To iTemp2
    If ((iTemp1 < 1) Or (iTemp2 > 14)) Begin
        Showln "Invalid source length for this example."
        Procedure_Return
    End

    Move 0 To iPosition
    While (iPosition < (ComLength(hoSizeText)))
        Get ComGetRange Of hoSizeText iPosition 1 False To sDigit
        Get ComContains Of hoDigits sDigit True To bTemp1
        If (bTemp1 <> True) Begin
            Showln "Source length is not decimal."
            Procedure_Return
        End

        Move (iPosition + 1) To iPosition
    Loop

    Move 0 To iFullBillions
    Move 0 To iTailBytes
    Get ComLength Of hoSizeText To iTemp1
    If (iTemp1 > 9) Begin
        Get ComGetRange Of hoSizeText 0 ((ComLength(hoSizeText)) - 9) False To sTemp1
        Get ComSetString Of hoPiece sTemp1 To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Procedure_Return
        End

        Get ComIntValue Of hoPiece To iFullBillions
        Get ComGetRange Of hoSizeText ((ComLength(hoSizeText)) - 9) 9 False To sTemp1
        Get ComSetString Of hoPiece sTemp1 To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Procedure_Return
        End

        Get ComIntValue Of hoPiece To iTailBytes
    End
    Else Begin
        Get ComIntValue Of hoSizeText To iTailBytes
    End

    Move iFullBillions To iNumParts
    If (iTailBytes > 0) Begin
        Move (iNumParts + 1) To iNumParts
    End

    If ((iNumParts < 1) Or (iNumParts > 10000)) Begin
        Showln "This example supports objects through 10 TB using 1 GB parts."
        Procedure_Return
    End

    If ((iFullBillions = 0) And (iTailBytes <= 5242880)) Begin
        Showln "Use CopyObject for sources of 5 MiB or less; ranged copy requires a larger source."
        Procedure_Return
    End

    Get Create (RefClass(cComCkFileAccess)) To hoFiles
    If (Not(IsComObjectCreated(hoFiles))) Begin
        Send CreateComObject of hoFiles
    End
    Get ComFileExists Of hoFiles "s3-multipart.json" To bTemp1
    If (bTemp1 = True) Begin
        Showln "A multipart state file already exists; finish or abort that upload first."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoCopySource
    If (Not(IsComObjectCreated(hoCopySource))) Begin
        Send CreateComObject of hoCopySource
    End
    Get ComAppend Of hoCopySource "/" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build text."
        Procedure_Return
    End

    Get ComAppend Of hoCopySource sBucketName To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build text."
        Procedure_Return
    End

    Get ComGetAsString Of hoObjectPath To sTemp1
    Get ComAppend Of hoCopySource sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build text."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComSetString Of hoTextCompare sSourceVersion To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText6
    If (iSameText6 <> True) Begin
        Get Create (RefClass(cComChilkatStringBuilder)) To hoEncodedVersion
        If (Not(IsComObjectCreated(hoEncodedVersion))) Begin
            Send CreateComObject of hoEncodedVersion
        End
        Get ComAppend Of hoEncodedVersion sSourceVersion To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not build text."
            Procedure_Return
        End

        Get ComEncode Of hoEncodedVersion "url_rfc3986" "utf-8" To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Procedure_Return
        End

        Get ComAppend Of hoCopySource "?versionId=" To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not build text."
            Procedure_Return
        End

        Get ComGetAsString Of hoEncodedVersion To sTemp1
        Get ComAppend Of hoCopySource sTemp1 To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not build text."
            Procedure_Return
        End

    End

    //  Destination is a DIFFERENT key in this bucket. Existing destination is replaced.
    Move "documents/copy.bin" To sDestinationKey
    Send ComClear To hoTextCompare
    Get ComSetString Of hoTextCompare sDestinationKey To iSuccess
    Get ComContentsEqual Of hoTextCompare sObjectKey True To iSameText7
    If (iSameText7 = True) Begin
        Showln "Choose a destination key different from the source."
        Procedure_Return
    End

    Get ComSetString Of hoObjectPath sDestinationKey To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComEncode Of hoObjectPath "url_rfc3986" "utf-8" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComReplace Of hoObjectPath "%2F" "/" To iDestinationSlashes
    Get ComPrepend Of hoObjectPath "/" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAddQueryParam Of hoRest "uploads" "" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "Content-Type" "application/octet-stream" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetAsString Of hoObjectPath To sTemp1
    Get ComFullRequestNoBody Of hoRest "POST" sTemp1 To sResponseBody
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Request did not complete; inspect diagnostics privately."
        Procedure_Return
    End

    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 iSameText8
        If ((iSuccess = True) And (iSameText8 = 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 ComLoadXml Of hoXml sResponseBody To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected well-formed XML."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComTag Of hoXml To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "InitiateMultipartUploadResult" True To iSameText9
    If (iSameText9 <> True) Begin
        Showln "Unexpected S3 response element; this is not a successful result."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComGetChildContent Of hoXml "UploadId" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText10
    Get ComHasChildWithTag Of hoXml "UploadId" To bTemp1
    If ((bTemp1 <> True) Or (iSameText10 = True)) Begin
        Showln "Missing response field: UploadId"
        Procedure_Return
    End

    Get ComGetChildContent Of hoXml "UploadId" To sUploadId
    Get Create (RefClass(cComChilkatJsonObject)) To hoState
    If (Not(IsComObjectCreated(hoState))) Begin
        Send CreateComObject of hoState
    End
    Get ComUpdateString Of hoState "bucket" sBucketName To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build saved state."
        Procedure_Return
    End

    Get ComUpdateString Of hoState "region" sRegion To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build saved state."
        Procedure_Return
    End

    Get ComUpdateString Of hoState "key" sDestinationKey To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build saved state."
        Procedure_Return
    End

    Get ComUpdateString Of hoState "upload_id" sUploadId To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build saved state."
        Procedure_Return
    End

    Get ComUpdateString Of hoState "source_key" sObjectKey To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build saved state."
        Procedure_Return
    End

    Get ComUpdateString Of hoState "source_etag" sSourceEtag To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build saved state."
        Procedure_Return
    End

    Get ComUpdateString Of hoState "source_size" sSourceSize To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build saved state."
        Procedure_Return
    End

    Get ComWriteFile Of hoState "s3-multipart.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Upload initiated but saving state failed; recover the upload ID or list unfinished uploads."
        Procedure_Return
    End

    Get ComRemoveQueryParam Of hoRest "uploads" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComRemoveHeader Of hoRest "Content-Type" 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 ComGetAsString Of hoCopySource To sTemp1
    Get ComAddHeader Of hoRest "x-amz-copy-source" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "x-amz-copy-source-if-match" sSourceEtag To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatXml)) To hoParts
    If (Not(IsComObjectCreated(hoParts))) Begin
        Send CreateComObject of hoParts
    End
    Set ComTag Of hoParts To "CompleteMultipartUpload"
    Get Create (RefClass(cComChilkatStringBuilder)) To hoRange
    If (Not(IsComObjectCreated(hoRange))) Begin
        Send CreateComObject of hoRange
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoEnding
    If (Not(IsComObjectCreated(hoEnding))) Begin
        Send CreateComObject of hoEnding
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoNumber
    If (Not(IsComObjectCreated(hoNumber))) Begin
        Send CreateComObject of hoNumber
    End
    Move 1 To iPartNumber
    While (iPartNumber <= iNumParts)
        Send ComClear To hoRange
        Send ComClear To hoEnding
        Send ComClear To hoNumber
        Get ComAppend Of hoRange "bytes=" To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not build text."
            Procedure_Return
        End

        If (iPartNumber = 1) Begin
            Get ComAppend Of hoRange "0" To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not build text."
                Procedure_Return
            End

        End
        Else Begin
            Get ComAppendInt Of hoRange (iPartNumber - 1) To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not prepare the request."
                Procedure_Return
            End

            Get ComAppend Of hoRange "000000000" To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not build text."
                Procedure_Return
            End

        End

        Get ComAppend Of hoRange "-" To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not build text."
            Procedure_Return
        End

        If (iPartNumber <= iFullBillions) Begin
            If (iPartNumber > 1) Begin
                Get ComAppendInt Of hoEnding (iPartNumber - 1) To iSuccess
                If (iSuccess <> True) Begin
                    Showln "Could not prepare the request."
                    Procedure_Return
                End

            End

            Get ComAppend Of hoEnding "999999999" To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not build text."
                Procedure_Return
            End

        End
        Else Begin
            Get ComAppendInt Of hoEnding (iTailBytes - 1) To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not prepare the request."
                Procedure_Return
            End

            If (iFullBillions > 0) Begin
                While ((ComLength(hoEnding)) < 9)
                    Get ComPrepend Of hoEnding "0" To iSuccess
                    If (iSuccess <> True) Begin
                        Showln "Could not prepare the request."
                        Procedure_Return
                    End

                Loop

                Get ComAppendInt Of hoNumber iFullBillions To iSuccess
                If (iSuccess <> True) Begin
                    Showln "Could not prepare the request."
                    Procedure_Return
                End

                Get ComGetAsString Of hoNumber To sTemp1
                Get ComPrepend Of hoEnding sTemp1 To iSuccess
                If (iSuccess <> True) Begin
                    Showln "Could not prepare the request."
                    Procedure_Return
                End

            End

        End

        Get ComGetAsString Of hoEnding To sTemp1
        Get ComAppend Of hoRange sTemp1 To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not build text."
            Procedure_Return
        End

        Send ComClear To hoNumber
        Get ComAppendInt Of hoNumber 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

            Get ComRemoveHeader Of hoRest "x-amz-copy-source-range" To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not prepare the request."
                Procedure_Return
            End

        End

        Get ComGetAsString Of hoNumber To sTemp1
        Get ComAddQueryParam Of hoRest "partNumber" sTemp1 To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Procedure_Return
        End

        Get ComGetAsString Of hoRange To sTemp1
        Get ComAddHeader Of hoRest "x-amz-copy-source-range" sTemp1 To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not prepare the request."
            Procedure_Return
        End

        Get ComGetAsString Of hoObjectPath To sTemp1
        Get ComFullRequestNoBody Of hoRest "PUT" sTemp1 To sResponseBody
        Get ComLastMethodSuccess Of hoRest To bTemp1
        If (bTemp1 <> True) Begin
            Showln "Request did not complete; inspect diagnostics privately."
            Procedure_Return
        End

        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 iSameText11
            If ((iSuccess = True) And (iSameText11 = 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 ComLoadXml Of hoXml sResponseBody To iSuccess
        If (iSuccess <> True) Begin
            Showln "Expected well-formed XML."
            Procedure_Return
        End

        Send ComClear To hoTextCompare
        Get ComTag Of hoXml To sTemp1
        Get ComSetString Of hoTextCompare sTemp1 To iSuccess
        Get ComContentsEqual Of hoTextCompare "CopyPartResult" True To iSameText12
        If (iSameText12 <> True) Begin
            Showln "Unexpected S3 response element; this is not a successful result."
            Procedure_Return
        End

        Send ComClear To hoTextCompare
        Get ComGetChildContent Of hoXml "ETag" To sTemp1
        Get ComSetString Of hoTextCompare sTemp1 To iSuccess
        Get ComContentsEqual Of hoTextCompare "" True To iSameText13
        Get ComHasChildWithTag Of hoXml "ETag" To bTemp1
        If ((bTemp1 <> True) Or (iSameText13 = True)) Begin
            Showln "Missing response field: ETag"
            Procedure_Return
        End

        Set ComI Of hoParts To iPartNumber - 1
        Send ComUpdateChildContentInt To hoParts "Part[i]|PartNumber" iPartNumber
        Get ComGetChildContent Of hoXml "ETag" To sTemp1
        Send ComUpdateChildContent To hoParts "Part[i]|ETag" sTemp1
        Showln "Copied part: " iPartNumber
        Move (iPartNumber + 1) To iPartNumber
    Loop

    Get ComGetXml Of hoParts To sTemp1
    Get ComUpdateString Of hoState "completion_xml" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build saved state."
        Procedure_Return
    End

    Get ComWriteFile Of hoState "s3-multipart.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Parts copied but completion state could not be saved."
        Procedure_Return
    End

    Get ComRemoveHeader Of hoRest "x-amz-copy-source" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComRemoveHeader Of hoRest "x-amz-copy-source-if-match" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComRemoveHeader Of hoRest "x-amz-copy-source-range" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComRemoveQueryParam Of hoRest "partNumber" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "Content-Type" "application/xml" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetAsString Of hoObjectPath To sTemp1
    Get ComGetXml Of hoParts To sTemp2
    Get ComFullRequestString Of hoRest "POST" sTemp1 sTemp2 To sResponseBody
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Request did not complete; inspect diagnostics privately."
        Procedure_Return
    End

    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 iSameText14
        If ((iSuccess = True) And (iSameText14 = 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 ComLoadXml Of hoXml sResponseBody To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected well-formed XML."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComTag Of hoXml To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "CompleteMultipartUploadResult" True To iSameText15
    If (iSameText15 <> True) Begin
        Showln "Unexpected S3 response element; this is not a successful result."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComGetChildContent Of hoXml "ETag" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText16
    Get ComHasChildWithTag Of hoXml "ETag" To bTemp1
    If ((bTemp1 <> True) Or (iSameText16 = True)) Begin
        Showln "Missing response field: ETag"
        Procedure_Return
    End

    Showln "Multipart copy completed; source was not deleted."
    //  Archive s3-multipart.json after confirming the destination.


End_Procedure