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

Amazon S3: Resume a Download with Range and If-Match

See more S3 - REST Examples

Resume an unchanged partial file using its original quoted ETag and total length, not metadata fetched after the failure. Save a private s3-download.json sidecar when starting: string fields bucket, key, region, etag (including quotes), and total_size. Use s3-download.part for that object only.

FileSizeStr avoids 32-bit overflow. The code appends only HTTP 206 with a matching ETag, Content-Range start and total size. A 200 response must never be appended; 412 means the validator failed, and 416 is not automatically success. Check final size, and verify an independently recorded checksum when needed. ETags are validators, not universally MD5 hashes. Concurrent local modification is outside this example.

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 GetObject 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 loCheckpoint
LOCAL lnSameText4
LOCAL lnSameText5
LOCAL lnSameText6
LOCAL lnSameText7
LOCAL lnSameText8
LOCAL lnSameText9
LOCAL lnSameText10
LOCAL lnSameText11
LOCAL loFiles
LOCAL lcLocalSize
LOCAL lnSameText12
LOCAL loRange
LOCAL lnStatusCode
LOCAL lcErrorBody
LOCAL lnSameText13
LOCAL lcReturnedEtag
LOCAL lnSameText14
LOCAL loContentRange
LOCAL loExpectedStart
LOCAL loExpectedTotal
LOCAL loOutput
LOCAL lcFinalSize
LOCAL lnSameText15

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/example.txt"
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

*  The sidecar must describe the SAME partial file and original object.
*  Save its exact quoted ETag and full Content-Length when starting the download.
loCheckpoint = CreateObject('Chilkat.JsonObject')
loCheckpoint.Strict = 1
lnSuccess = loCheckpoint.LoadFile("s3-download.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load the download checkpoint."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loCheckpoint.StringOf("bucket"))
lnSameText4 = loTextCompare.ContentsEqual("",1)
IF ((loCheckpoint.JsonTypeOf("bucket") <> 1) OR (lnSameText4 = 1)) THEN
    ? "Invalid checkpoint field: bucket"
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loCheckpoint.StringOf("key"))
lnSameText5 = loTextCompare.ContentsEqual("",1)
IF ((loCheckpoint.JsonTypeOf("key") <> 1) OR (lnSameText5 = 1)) THEN
    ? "Invalid checkpoint field: key"
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loCheckpoint.StringOf("region"))
lnSameText6 = loTextCompare.ContentsEqual("",1)
IF ((loCheckpoint.JsonTypeOf("region") <> 1) OR (lnSameText6 = 1)) THEN
    ? "Invalid checkpoint field: region"
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loCheckpoint.StringOf("etag"))
lnSameText7 = loTextCompare.ContentsEqual("",1)
IF ((loCheckpoint.JsonTypeOf("etag") <> 1) OR (lnSameText7 = 1)) THEN
    ? "Invalid checkpoint field: etag"
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loCheckpoint.StringOf("total_size"))
lnSameText8 = loTextCompare.ContentsEqual("",1)
IF ((loCheckpoint.JsonTypeOf("total_size") <> 1) OR (lnSameText8 = 1)) THEN
    ? "Invalid checkpoint field: total_size"
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loCheckpoint.StringOf("bucket"))
lnSameText9 = loTextCompare.ContentsEqual(lcBucketName,1)
loTextCompare.Clear()
loTextCompare.SetString(loCheckpoint.StringOf("key"))
lnSameText10 = loTextCompare.ContentsEqual(lcObjectKey,1)
loTextCompare.Clear()
loTextCompare.SetString(loCheckpoint.StringOf("region"))
lnSameText11 = loTextCompare.ContentsEqual(lcRegion,1)
IF ((lnSameText9 <> 1) OR (lnSameText10 <> 1) OR (lnSameText11 <> 1)) THEN
    ? "Checkpoint does not match this object."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    CANCEL
ENDIF

loFiles = CreateObject('Chilkat.FileAccess')
lcLocalSize = loFiles.FileSizeStr("s3-download.part")
IF (loFiles.LastMethodSuccess <> 1) THEN
    ? "Could not determine the partial file size."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    RELEASE loFiles
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(lcLocalSize)
lnSameText12 = loTextCompare.ContentsEqual(loCheckpoint.StringOf("total_size"),1)
IF (lnSameText12 = 1) THEN
    ? "Local length already matches the checkpoint; verify integrity before publishing."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    RELEASE loFiles
    CANCEL
ENDIF

loRange = CreateObject('Chilkat.StringBuilder')
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 loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    CANCEL
ENDIF

lnSuccess = loRange.Append(lcLocalSize)
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 loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    CANCEL
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 loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    CANCEL
ENDIF

lnSuccess = loRest.AddHeader("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 loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    CANCEL
ENDIF

lnSuccess = loRest.AddHeader("If-Match",loCheckpoint.StringOf("etag"))
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 loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    CANCEL
ENDIF

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

lnStatusCode = loRest.ReadResponseHeader()
IF (lnStatusCode < 0) THEN
    ? "Could not read download headers."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    CANCEL
ENDIF

IF (lnStatusCode <> 206) THEN
    ? "Download HTTP status: " + STR(lnStatusCode)
    lcErrorBody = loRest.ReadRespBodyString()
    IF (loRest.LastMethodSuccess = 1) THEN
        lnSuccess = loXml.LoadXml(lcErrorBody)
        loTextCompare.Clear()
        loTextCompare.SetString(loXml.Tag)
        lnSameText13 = loTextCompare.ContentsEqual("Error",1)
        IF ((lnSuccess = 1) AND (lnSameText13 = 1)) THEN
            ? "S3 error code: " + loXml.GetChildContent("Code")
        ENDIF

    ENDIF

    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    CANCEL
ENDIF

*  Never append HTTP 200, 412, or 416 to the existing file.
lcReturnedEtag = loRest.ResponseHdrByName("ETag")
loTextCompare.Clear()
loTextCompare.SetString(lcReturnedEtag)
lnSameText14 = loTextCompare.ContentsEqual(loCheckpoint.StringOf("etag"),1)
IF ((loRest.LastMethodSuccess <> 1) OR (lnSameText14 <> 1)) THEN
    ? "Object validator changed; partial file was not modified."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    CANCEL
ENDIF

loContentRange = CreateObject('Chilkat.StringBuilder')
lnSuccess = loContentRange.Append(loRest.ResponseHdrByName("Content-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 loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    RELEASE loContentRange
    CANCEL
ENDIF

loExpectedStart = CreateObject('Chilkat.StringBuilder')
lnSuccess = loExpectedStart.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 loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    RELEASE loContentRange
    RELEASE loExpectedStart
    CANCEL
ENDIF

lnSuccess = loExpectedStart.Append(lcLocalSize)
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 loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    RELEASE loContentRange
    RELEASE loExpectedStart
    CANCEL
ENDIF

lnSuccess = loExpectedStart.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 loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    RELEASE loContentRange
    RELEASE loExpectedStart
    CANCEL
ENDIF

IF (loContentRange.StartsWith(loExpectedStart.GetAsString(),1) <> 1) THEN
    ? "Range start does not match the partial file."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    RELEASE loContentRange
    RELEASE loExpectedStart
    CANCEL
ENDIF

loExpectedTotal = CreateObject('Chilkat.StringBuilder')
lnSuccess = loExpectedTotal.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 loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    RELEASE loContentRange
    RELEASE loExpectedStart
    RELEASE loExpectedTotal
    CANCEL
ENDIF

lnSuccess = loExpectedTotal.Append(loCheckpoint.StringOf("total_size"))
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 loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    RELEASE loContentRange
    RELEASE loExpectedStart
    RELEASE loExpectedTotal
    CANCEL
ENDIF

IF (loContentRange.EndsWith(loExpectedTotal.GetAsString(),1) <> 1) THEN
    ? "Object length changed; partial file was not modified."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    RELEASE loContentRange
    RELEASE loExpectedStart
    RELEASE loExpectedTotal
    CANCEL
ENDIF

*  The file must be an unchanged prefix of the object; size alone cannot prove that.
loOutput = CreateObject('Chilkat.Stream')
loOutput.SinkFile = "s3-download.part"
loOutput.SinkFileAppend = 1
lnSuccess = loRest.ReadRespBodyStream(loOutput,1)
IF (lnSuccess <> 1) THEN
    ? "Resume incomplete; retain the partial file and original checkpoint."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    RELEASE loContentRange
    RELEASE loExpectedStart
    RELEASE loExpectedTotal
    RELEASE loOutput
    CANCEL
ENDIF

lcFinalSize = loFiles.FileSizeStr("s3-download.part")
loTextCompare.Clear()
loTextCompare.SetString(lcFinalSize)
lnSameText15 = loTextCompare.ContentsEqual(loCheckpoint.StringOf("total_size"),1)
IF ((loFiles.LastMethodSuccess <> 1) OR (lnSameText15 <> 1)) THEN
    ? "Result length does not match the original full object."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loObjectPath
    RELEASE loCheckpoint
    RELEASE loFiles
    RELEASE loRange
    RELEASE loContentRange
    RELEASE loExpectedStart
    RELEASE loExpectedTotal
    RELEASE loOutput
    CANCEL
ENDIF

? "Resumed download complete."

RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loObjectPath
RELEASE loCheckpoint
RELEASE loFiles
RELEASE loRange
RELEASE loContentRange
RELEASE loExpectedStart
RELEASE loExpectedTotal
RELEASE loOutput