Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
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 lcObjectKey
LOCAL loObjectPath
LOCAL lnRestoredSlashes
LOCAL lnStatusCode
LOCAL lcSourceEtag
LOCAL lnSameText4
LOCAL lcSourceSize
LOCAL lnSameText5
LOCAL lcSourceVersion
LOCAL loSizeText
LOCAL loDigits
LOCAL loPiece
LOCAL lnPosition
LOCAL lcDigit
LOCAL lnFullBillions
LOCAL lnTailBytes
LOCAL lnNumParts
LOCAL loFiles
LOCAL loCopySource
LOCAL lnSameText6
LOCAL loEncodedVersion
LOCAL lcDestinationKey
LOCAL lnSameText7
LOCAL lnDestinationSlashes
LOCAL lcResponseBody
LOCAL lnSameText8
LOCAL lnSameText9
LOCAL lnSameText10
LOCAL lcUploadId
LOCAL loState
LOCAL loParts
LOCAL loRange
LOCAL loEnding
LOCAL loNumber
LOCAL lnPartNumber
LOCAL lnSameText11
LOCAL lnSameText12
LOCAL lnSameText13
LOCAL lnSameText14
LOCAL lnSameText15
LOCAL lnSameText16

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
*  Supply an unencoded object key, not a URL. Preserve slashes in the key.
lcObjectKey = "documents/source.bin"
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 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 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 loObjectPath
    CANCEL
ENDIF

lnSuccess = loRest.SendReqNoBody("HEAD",loObjectPath.GetAsString())
IF (lnSuccess <> 1) THEN
    ? "Could not send HEAD request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    CANCEL
ENDIF

lnStatusCode = loRest.ReadResponseHeader()
IF (lnStatusCode < 0) THEN
    ? "Could not read the response header."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    CANCEL
ENDIF

IF (lnStatusCode <> 200) THEN
    ? "HEAD did not return HTTP 200. A 403 is not proof that an object is missing."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    CANCEL
ENDIF

*  HEAD has no response body; do not call a body-reading method.
lcSourceEtag = loRest.ResponseHdrByName("ETag")
loTextCompare.Clear()
loTextCompare.SetString(lcSourceEtag)
lnSameText4 = loTextCompare.ContentsEqual("",1)
IF ((loRest.LastMethodSuccess <> 1) OR (lnSameText4 = 1)) THEN
    ? "Missing response header: ETag"
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    CANCEL
ENDIF

lcSourceSize = loRest.ResponseHdrByName("Content-Length")
loTextCompare.Clear()
loTextCompare.SetString(lcSourceSize)
lnSameText5 = loTextCompare.ContentsEqual("",1)
IF ((loRest.LastMethodSuccess <> 1) OR (lnSameText5 = 1)) THEN
    ? "Missing response header: Content-Length"
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    CANCEL
ENDIF

lcSourceVersion = loRest.ResponseHdrByName("x-amz-version-id")
IF (loRest.LastMethodSuccess <> 1) THEN
    lcSourceVersion = ""
ENDIF

*  Split a decimal size into billions and a remainder. No large integer type.
loSizeText = CreateObject('Chilkat.StringBuilder')
loDigits = CreateObject('Chilkat.StringBuilder')
loPiece = CreateObject('Chilkat.StringBuilder')
lnSuccess = loSizeText.Append(lcSourceSize)
IF (lnSuccess <> 1) THEN
    ? "Could not build text."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    CANCEL
ENDIF

lnSuccess = loDigits.Append("0123456789")
IF (lnSuccess <> 1) THEN
    ? "Could not build text."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    CANCEL
ENDIF

IF ((loSizeText.Length < 1) OR (loSizeText.Length > 14)) THEN
    ? "Invalid source length for this example."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    CANCEL
ENDIF

lnPosition = 0
DO WHILE (lnPosition < loSizeText.Length)
    lcDigit = loSizeText.GetRange(lnPosition,1,0)
    IF (loDigits.Contains(lcDigit,1) <> 1) THEN
        ? "Source length is not decimal."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        CANCEL
    ENDIF

    lnPosition = lnPosition + 1
ENDDO
lnFullBillions = 0
lnTailBytes = 0
IF (loSizeText.Length > 9) THEN
    lnSuccess = loPiece.SetString(loSizeText.GetRange(0,loSizeText.Length - 9,0))
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        CANCEL
    ENDIF

    lnFullBillions = loPiece.IntValue
    lnSuccess = loPiece.SetString(loSizeText.GetRange(loSizeText.Length - 9,9,0))
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        CANCEL
    ENDIF

    lnTailBytes = loPiece.IntValue
ELSE
    lnTailBytes = loSizeText.IntValue
ENDIF

lnNumParts = lnFullBillions
IF (lnTailBytes > 0) THEN
    lnNumParts = lnNumParts + 1
ENDIF

IF ((lnNumParts < 1) OR (lnNumParts > 10000)) THEN
    ? "This example supports objects through 10 TB using 1 GB parts."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    CANCEL
ENDIF

IF ((lnFullBillions = 0) AND (lnTailBytes <= 5242880)) THEN
    ? "Use CopyObject for sources of 5 MiB or less; ranged copy requires a larger source."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    CANCEL
ENDIF

loFiles = CreateObject('Chilkat.FileAccess')
IF (loFiles.FileExists("s3-multipart.json") = 1) THEN
    ? "A multipart state file already exists; finish or abort that upload first."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    CANCEL
ENDIF

loCopySource = CreateObject('Chilkat.StringBuilder')
lnSuccess = loCopySource.Append("/")
IF (lnSuccess <> 1) THEN
    ? "Could not build text."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    CANCEL
ENDIF

lnSuccess = loCopySource.Append(lcBucketName)
IF (lnSuccess <> 1) THEN
    ? "Could not build text."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    CANCEL
ENDIF

lnSuccess = loCopySource.Append(loObjectPath.GetAsString())
IF (lnSuccess <> 1) THEN
    ? "Could not build text."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(lcSourceVersion)
lnSameText6 = loTextCompare.ContentsEqual("",1)
IF (lnSameText6 <> 1) THEN
    loEncodedVersion = CreateObject('Chilkat.StringBuilder')
    lnSuccess = loEncodedVersion.Append(lcSourceVersion)
    IF (lnSuccess <> 1) THEN
        ? "Could not build text."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        CANCEL
    ENDIF

    lnSuccess = loEncodedVersion.Encode("url_rfc3986","utf-8")
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        CANCEL
    ENDIF

    lnSuccess = loCopySource.Append("?versionId=")
    IF (lnSuccess <> 1) THEN
        ? "Could not build text."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        CANCEL
    ENDIF

    lnSuccess = loCopySource.Append(loEncodedVersion.GetAsString())
    IF (lnSuccess <> 1) THEN
        ? "Could not build text."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        CANCEL
    ENDIF

ENDIF

*  Destination is a DIFFERENT key in this bucket. Existing destination is replaced.
lcDestinationKey = "documents/copy.bin"
loTextCompare.Clear()
loTextCompare.SetString(lcDestinationKey)
lnSameText7 = loTextCompare.ContentsEqual(lcObjectKey,1)
IF (lnSameText7 = 1) THEN
    ? "Choose a destination key different from the source."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    CANCEL
ENDIF

lnSuccess = loObjectPath.SetString(lcDestinationKey)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    CANCEL
ENDIF

lnSuccess = loObjectPath.Encode("url_rfc3986","utf-8")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    CANCEL
ENDIF

lnDestinationSlashes = 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 loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("uploads","")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    CANCEL
ENDIF

lnSuccess = loRest.AddHeader("Content-Type","application/octet-stream")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    CANCEL
ENDIF

lcResponseBody = loRest.FullRequestNoBody("POST",loObjectPath.GetAsString())
IF (loRest.LastMethodSuccess <> 1) THEN
    ? "Request did not complete; inspect diagnostics privately."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode <> 200) THEN
    ? "S3 HTTP status: " + STR(loRest.ResponseStatusCode)
    lnSuccess = loXml.LoadXml(lcResponseBody)
    loTextCompare.Clear()
    loTextCompare.SetString(loXml.Tag)
    lnSameText8 = loTextCompare.ContentsEqual("Error",1)
    IF ((lnSuccess = 1) AND (lnSameText8 = 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 loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    CANCEL
ENDIF

lnSuccess = loXml.LoadXml(lcResponseBody)
IF (lnSuccess <> 1) THEN
    ? "Expected well-formed XML."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loXml.Tag)
lnSameText9 = loTextCompare.ContentsEqual("InitiateMultipartUploadResult",1)
IF (lnSameText9 <> 1) THEN
    ? "Unexpected S3 response element; this is not a successful result."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loXml.GetChildContent("UploadId"))
lnSameText10 = loTextCompare.ContentsEqual("",1)
IF ((loXml.HasChildWithTag("UploadId") <> 1) OR (lnSameText10 = 1)) THEN
    ? "Missing response field: UploadId"
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    CANCEL
ENDIF

lcUploadId = loXml.GetChildContent("UploadId")
loState = CreateObject('Chilkat.JsonObject')
lnSuccess = loState.UpdateString("bucket",lcBucketName)
IF (lnSuccess <> 1) THEN
    ? "Could not build saved state."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

lnSuccess = loState.UpdateString("region",lcRegion)
IF (lnSuccess <> 1) THEN
    ? "Could not build saved state."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

lnSuccess = loState.UpdateString("key",lcDestinationKey)
IF (lnSuccess <> 1) THEN
    ? "Could not build saved state."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

lnSuccess = loState.UpdateString("upload_id",lcUploadId)
IF (lnSuccess <> 1) THEN
    ? "Could not build saved state."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

lnSuccess = loState.UpdateString("source_key",lcObjectKey)
IF (lnSuccess <> 1) THEN
    ? "Could not build saved state."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

lnSuccess = loState.UpdateString("source_etag",lcSourceEtag)
IF (lnSuccess <> 1) THEN
    ? "Could not build saved state."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

lnSuccess = loState.UpdateString("source_size",lcSourceSize)
IF (lnSuccess <> 1) THEN
    ? "Could not build saved state."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

lnSuccess = loState.WriteFile("s3-multipart.json")
IF (lnSuccess <> 1) THEN
    ? "Upload initiated but saving state failed; recover the upload ID or list unfinished uploads."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

lnSuccess = loRest.RemoveQueryParam("uploads")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

lnSuccess = loRest.RemoveHeader("Content-Type")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    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 loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

lnSuccess = loRest.AddHeader("x-amz-copy-source",loCopySource.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 loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

lnSuccess = loRest.AddHeader("x-amz-copy-source-if-match",lcSourceEtag)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    CANCEL
ENDIF

loParts = CreateObject('Chilkat.Xml')
loParts.Tag = "CompleteMultipartUpload"
loRange = CreateObject('Chilkat.StringBuilder')
loEnding = CreateObject('Chilkat.StringBuilder')
loNumber = CreateObject('Chilkat.StringBuilder')
lnPartNumber = 1
DO WHILE (lnPartNumber <= lnNumParts)
    loRange.Clear()
    loEnding.Clear()
    loNumber.Clear()
    lnSuccess = loRange.Append("bytes=")
    IF (lnSuccess <> 1) THEN
        ? "Could not build text."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        RELEASE loState
        RELEASE loParts
        RELEASE loRange
        RELEASE loEnding
        RELEASE loNumber
        CANCEL
    ENDIF

    IF (lnPartNumber = 1) THEN
        lnSuccess = loRange.Append("0")
        IF (lnSuccess <> 1) THEN
            ? "Could not build text."
            RELEASE loTextCompare
            RELEASE loCredentials
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            RELEASE loObjectPath
            RELEASE loSizeText
            RELEASE loDigits
            RELEASE loPiece
            RELEASE loFiles
            RELEASE loCopySource
            RELEASE loEncodedVersion
            RELEASE loState
            RELEASE loParts
            RELEASE loRange
            RELEASE loEnding
            RELEASE loNumber
            CANCEL
        ENDIF

    ELSE
        lnSuccess = loRange.AppendInt(lnPartNumber - 1)
        IF (lnSuccess <> 1) THEN
            ? "Could not prepare the request."
            RELEASE loTextCompare
            RELEASE loCredentials
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            RELEASE loObjectPath
            RELEASE loSizeText
            RELEASE loDigits
            RELEASE loPiece
            RELEASE loFiles
            RELEASE loCopySource
            RELEASE loEncodedVersion
            RELEASE loState
            RELEASE loParts
            RELEASE loRange
            RELEASE loEnding
            RELEASE loNumber
            CANCEL
        ENDIF

        lnSuccess = loRange.Append("000000000")
        IF (lnSuccess <> 1) THEN
            ? "Could not build text."
            RELEASE loTextCompare
            RELEASE loCredentials
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            RELEASE loObjectPath
            RELEASE loSizeText
            RELEASE loDigits
            RELEASE loPiece
            RELEASE loFiles
            RELEASE loCopySource
            RELEASE loEncodedVersion
            RELEASE loState
            RELEASE loParts
            RELEASE loRange
            RELEASE loEnding
            RELEASE loNumber
            CANCEL
        ENDIF

    ENDIF

    lnSuccess = loRange.Append("-")
    IF (lnSuccess <> 1) THEN
        ? "Could not build text."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        RELEASE loState
        RELEASE loParts
        RELEASE loRange
        RELEASE loEnding
        RELEASE loNumber
        CANCEL
    ENDIF

    IF (lnPartNumber <= lnFullBillions) THEN
        IF (lnPartNumber > 1) THEN
            lnSuccess = loEnding.AppendInt(lnPartNumber - 1)
            IF (lnSuccess <> 1) THEN
                ? "Could not prepare the request."
                RELEASE loTextCompare
                RELEASE loCredentials
                RELEASE loEndpoint
                RELEASE loAws
                RELEASE loRest
                RELEASE loConnection
                RELEASE loXml
                RELEASE loObjectPath
                RELEASE loSizeText
                RELEASE loDigits
                RELEASE loPiece
                RELEASE loFiles
                RELEASE loCopySource
                RELEASE loEncodedVersion
                RELEASE loState
                RELEASE loParts
                RELEASE loRange
                RELEASE loEnding
                RELEASE loNumber
                CANCEL
            ENDIF

        ENDIF

        lnSuccess = loEnding.Append("999999999")
        IF (lnSuccess <> 1) THEN
            ? "Could not build text."
            RELEASE loTextCompare
            RELEASE loCredentials
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            RELEASE loObjectPath
            RELEASE loSizeText
            RELEASE loDigits
            RELEASE loPiece
            RELEASE loFiles
            RELEASE loCopySource
            RELEASE loEncodedVersion
            RELEASE loState
            RELEASE loParts
            RELEASE loRange
            RELEASE loEnding
            RELEASE loNumber
            CANCEL
        ENDIF

    ELSE
        lnSuccess = loEnding.AppendInt(lnTailBytes - 1)
        IF (lnSuccess <> 1) THEN
            ? "Could not prepare the request."
            RELEASE loTextCompare
            RELEASE loCredentials
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            RELEASE loObjectPath
            RELEASE loSizeText
            RELEASE loDigits
            RELEASE loPiece
            RELEASE loFiles
            RELEASE loCopySource
            RELEASE loEncodedVersion
            RELEASE loState
            RELEASE loParts
            RELEASE loRange
            RELEASE loEnding
            RELEASE loNumber
            CANCEL
        ENDIF

        IF (lnFullBillions > 0) THEN
            DO WHILE (loEnding.Length < 9)
                lnSuccess = loEnding.Prepend("0")
                IF (lnSuccess <> 1) THEN
                    ? "Could not prepare the request."
                    RELEASE loTextCompare
                    RELEASE loCredentials
                    RELEASE loEndpoint
                    RELEASE loAws
                    RELEASE loRest
                    RELEASE loConnection
                    RELEASE loXml
                    RELEASE loObjectPath
                    RELEASE loSizeText
                    RELEASE loDigits
                    RELEASE loPiece
                    RELEASE loFiles
                    RELEASE loCopySource
                    RELEASE loEncodedVersion
                    RELEASE loState
                    RELEASE loParts
                    RELEASE loRange
                    RELEASE loEnding
                    RELEASE loNumber
                    CANCEL
                ENDIF

            ENDDO
            lnSuccess = loNumber.AppendInt(lnFullBillions)
            IF (lnSuccess <> 1) THEN
                ? "Could not prepare the request."
                RELEASE loTextCompare
                RELEASE loCredentials
                RELEASE loEndpoint
                RELEASE loAws
                RELEASE loRest
                RELEASE loConnection
                RELEASE loXml
                RELEASE loObjectPath
                RELEASE loSizeText
                RELEASE loDigits
                RELEASE loPiece
                RELEASE loFiles
                RELEASE loCopySource
                RELEASE loEncodedVersion
                RELEASE loState
                RELEASE loParts
                RELEASE loRange
                RELEASE loEnding
                RELEASE loNumber
                CANCEL
            ENDIF

            lnSuccess = loEnding.Prepend(loNumber.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 loObjectPath
                RELEASE loSizeText
                RELEASE loDigits
                RELEASE loPiece
                RELEASE loFiles
                RELEASE loCopySource
                RELEASE loEncodedVersion
                RELEASE loState
                RELEASE loParts
                RELEASE loRange
                RELEASE loEnding
                RELEASE loNumber
                CANCEL
            ENDIF

        ENDIF

    ENDIF

    lnSuccess = loRange.Append(loEnding.GetAsString())
    IF (lnSuccess <> 1) THEN
        ? "Could not build text."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        RELEASE loState
        RELEASE loParts
        RELEASE loRange
        RELEASE loEnding
        RELEASE loNumber
        CANCEL
    ENDIF

    loNumber.Clear()
    lnSuccess = loNumber.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 loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        RELEASE loState
        RELEASE loParts
        RELEASE loRange
        RELEASE loEnding
        RELEASE loNumber
        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 loObjectPath
            RELEASE loSizeText
            RELEASE loDigits
            RELEASE loPiece
            RELEASE loFiles
            RELEASE loCopySource
            RELEASE loEncodedVersion
            RELEASE loState
            RELEASE loParts
            RELEASE loRange
            RELEASE loEnding
            RELEASE loNumber
            CANCEL
        ENDIF

        lnSuccess = loRest.RemoveHeader("x-amz-copy-source-range")
        IF (lnSuccess <> 1) THEN
            ? "Could not prepare the request."
            RELEASE loTextCompare
            RELEASE loCredentials
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            RELEASE loObjectPath
            RELEASE loSizeText
            RELEASE loDigits
            RELEASE loPiece
            RELEASE loFiles
            RELEASE loCopySource
            RELEASE loEncodedVersion
            RELEASE loState
            RELEASE loParts
            RELEASE loRange
            RELEASE loEnding
            RELEASE loNumber
            CANCEL
        ENDIF

    ENDIF

    lnSuccess = loRest.AddQueryParam("partNumber",loNumber.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 loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        RELEASE loState
        RELEASE loParts
        RELEASE loRange
        RELEASE loEnding
        RELEASE loNumber
        CANCEL
    ENDIF

    lnSuccess = loRest.AddHeader("x-amz-copy-source-range",loRange.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 loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        RELEASE loState
        RELEASE loParts
        RELEASE loRange
        RELEASE loEnding
        RELEASE loNumber
        CANCEL
    ENDIF

    lcResponseBody = loRest.FullRequestNoBody("PUT",loObjectPath.GetAsString())
    IF (loRest.LastMethodSuccess <> 1) THEN
        ? "Request did not complete; inspect diagnostics privately."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        RELEASE loState
        RELEASE loParts
        RELEASE loRange
        RELEASE loEnding
        RELEASE loNumber
        CANCEL
    ENDIF

    IF (loRest.ResponseStatusCode <> 200) THEN
        ? "S3 HTTP status: " + STR(loRest.ResponseStatusCode)
        lnSuccess = loXml.LoadXml(lcResponseBody)
        loTextCompare.Clear()
        loTextCompare.SetString(loXml.Tag)
        lnSameText11 = loTextCompare.ContentsEqual("Error",1)
        IF ((lnSuccess = 1) AND (lnSameText11 = 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 loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        RELEASE loState
        RELEASE loParts
        RELEASE loRange
        RELEASE loEnding
        RELEASE loNumber
        CANCEL
    ENDIF

    lnSuccess = loXml.LoadXml(lcResponseBody)
    IF (lnSuccess <> 1) THEN
        ? "Expected well-formed XML."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        RELEASE loState
        RELEASE loParts
        RELEASE loRange
        RELEASE loEnding
        RELEASE loNumber
        CANCEL
    ENDIF

    loTextCompare.Clear()
    loTextCompare.SetString(loXml.Tag)
    lnSameText12 = loTextCompare.ContentsEqual("CopyPartResult",1)
    IF (lnSameText12 <> 1) THEN
        ? "Unexpected S3 response element; this is not a successful result."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        RELEASE loState
        RELEASE loParts
        RELEASE loRange
        RELEASE loEnding
        RELEASE loNumber
        CANCEL
    ENDIF

    loTextCompare.Clear()
    loTextCompare.SetString(loXml.GetChildContent("ETag"))
    lnSameText13 = loTextCompare.ContentsEqual("",1)
    IF ((loXml.HasChildWithTag("ETag") <> 1) OR (lnSameText13 = 1)) THEN
        ? "Missing response field: ETag"
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loObjectPath
        RELEASE loSizeText
        RELEASE loDigits
        RELEASE loPiece
        RELEASE loFiles
        RELEASE loCopySource
        RELEASE loEncodedVersion
        RELEASE loState
        RELEASE loParts
        RELEASE loRange
        RELEASE loEnding
        RELEASE loNumber
        CANCEL
    ENDIF

    loParts.I = lnPartNumber - 1
    loParts.UpdateChildContentInt("Part[i]|PartNumber",lnPartNumber)
    loParts.UpdateChildContent("Part[i]|ETag",loXml.GetChildContent("ETag"))
    ? "Copied part: " + STR(lnPartNumber)
    lnPartNumber = lnPartNumber + 1
ENDDO
lnSuccess = loState.UpdateString("completion_xml",loParts.GetXml())
IF (lnSuccess <> 1) THEN
    ? "Could not build saved state."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

lnSuccess = loState.WriteFile("s3-multipart.json")
IF (lnSuccess <> 1) THEN
    ? "Parts copied but completion state could not be saved."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

lnSuccess = loRest.RemoveHeader("x-amz-copy-source")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

lnSuccess = loRest.RemoveHeader("x-amz-copy-source-if-match")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

lnSuccess = loRest.RemoveHeader("x-amz-copy-source-range")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

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 loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

lnSuccess = loRest.AddHeader("Content-Type","application/xml")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

lcResponseBody = loRest.FullRequestString("POST",loObjectPath.GetAsString(),loParts.GetXml())
IF (loRest.LastMethodSuccess <> 1) THEN
    ? "Request did not complete; inspect diagnostics privately."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode <> 200) THEN
    ? "S3 HTTP status: " + STR(loRest.ResponseStatusCode)
    lnSuccess = loXml.LoadXml(lcResponseBody)
    loTextCompare.Clear()
    loTextCompare.SetString(loXml.Tag)
    lnSameText14 = loTextCompare.ContentsEqual("Error",1)
    IF ((lnSuccess = 1) AND (lnSameText14 = 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 loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

lnSuccess = loXml.LoadXml(lcResponseBody)
IF (lnSuccess <> 1) THEN
    ? "Expected well-formed XML."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loXml.Tag)
lnSameText15 = loTextCompare.ContentsEqual("CompleteMultipartUploadResult",1)
IF (lnSameText15 <> 1) THEN
    ? "Unexpected S3 response element; this is not a successful result."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loXml.GetChildContent("ETag"))
lnSameText16 = loTextCompare.ContentsEqual("",1)
IF ((loXml.HasChildWithTag("ETag") <> 1) OR (lnSameText16 = 1)) THEN
    ? "Missing response field: ETag"
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loSizeText
    RELEASE loDigits
    RELEASE loPiece
    RELEASE loFiles
    RELEASE loCopySource
    RELEASE loEncodedVersion
    RELEASE loState
    RELEASE loParts
    RELEASE loRange
    RELEASE loEnding
    RELEASE loNumber
    CANCEL
ENDIF

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

RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loSizeText
RELEASE loDigits
RELEASE loPiece
RELEASE loFiles
RELEASE loCopySource
RELEASE loEncodedVersion
RELEASE loState
RELEASE loParts
RELEASE loRange
RELEASE loEnding
RELEASE loNumber