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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_TextCompare
oleobject loo_Credentials
long li_SameText1
long li_SameText2
string ls_Region
string ls_BucketName
oleobject loo_Endpoint
oleobject loo_Aws
oleobject loo_Rest
long li_SameText3
oleobject loo_Connection
oleobject loo_Xml
string ls_ObjectKey
oleobject loo_ObjectPath
long li_RestoredSlashes
oleobject loo_Checkpoint
long li_SameText4
long li_SameText5
long li_SameText6
long li_SameText7
long li_SameText8
long li_SameText9
long li_SameText10
long li_SameText11
oleobject loo_Files
string ls_LocalSize
long li_SameText12
oleobject loo_Range
long li_StatusCode
string ls_ErrorBody
long li_SameText13
string ls_ReturnedEtag
long li_SameText14
oleobject loo_ContentRange
oleobject loo_ExpectedStart
oleobject loo_ExpectedTotal
oleobject loo_Output
string ls_FinalSize
long li_SameText15

li_Success = 0

//  StringBuilder compares text by value in every generated language.
loo_TextCompare = create oleobject
li_rc = loo_TextCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_TextCompare
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
//  Private configuration, not a file in public web content or source control.
//  Load current IAM credentials authorized for the S3 operation.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")

loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/s3.json")
if li_Success <> 1 then
    Write-Debug "Could not load private AWS credentials."
    destroy loo_TextCompare
    destroy loo_Credentials
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("access_key"))
li_SameText1 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_SameText1 = 1) then
    Write-Debug "Missing or invalid access_key."
    destroy loo_TextCompare
    destroy loo_Credentials
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_SameText2 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_SameText2 = 1) then
    Write-Debug "Missing or invalid secret_key."
    destroy loo_TextCompare
    destroy loo_Credentials
    return
end if

//  General purpose bucket in the commercial AWS partition.
ls_Region = "us-west-2"
ls_BucketName = "amzn-s3-demo-bucket"
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Endpoint.Append(ls_BucketName)
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append(".")
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append("s3.")
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append(ls_Region)
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append(".amazonaws.com")
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

loo_Aws = create oleobject
li_rc = loo_Aws.ConnectToNewObject("Chilkat.AuthAws")

loo_Aws.AccessKey = loo_Credentials.StringOf("access_key")
loo_Aws.SecretKey = loo_Credentials.StringOf("secret_key")
loo_Aws.Region = ls_Region
loo_Aws.ServiceName = "s3"
loo_Aws.SignatureVersion = 4
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
li_Success = loo_Rest.SetAuthAws(loo_Aws)
if li_Success <> 1 then
    Write-Debug "Could not configure AWS signing."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    return
end if

//  Temporary credentials require this token in every signed query.
//  Keep request URLs private. Do not clear this parameter between requests.
if loo_Credentials.HasMember("session_token") = 1 then
    if loo_Credentials.JsonTypeOf("session_token") <> 1 then
        Write-Debug "session_token must be a string."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        return
    end if

    loo_TextCompare.Clear()
    loo_TextCompare.SetString(loo_Credentials.StringOf("session_token"))
    li_SameText3 = loo_TextCompare.ContentsEqual("",1)
    if li_SameText3 <> 1 then
        li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",loo_Credentials.StringOf("session_token"))
        if li_Success <> 1 then
            Write-Debug "Could not prepare the request."
            destroy loo_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            return
        end if

    end if

end if

//  Connect to the SAME hostname used for TLS verification and request signing.
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
    Write-Debug "TLS connection failed; inspect diagnostics privately."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

li_Success = loo_Rest.UseConnection(loo_Connection,1)
if li_Success <> 1 then
    Write-Debug "Could not attach the TLS connection."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.Strict = 1
//  Supply an unencoded object key, not a URL. Preserve slashes in the key.
ls_ObjectKey = "documents/example.txt"
loo_ObjectPath = create oleobject
li_rc = loo_ObjectPath.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_ObjectPath.Append(ls_ObjectKey)
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    return
end if

li_Success = loo_ObjectPath.Encode("url_rfc3986","utf-8")
if li_Success <> 1 then
    Write-Debug "Could not encode the object key."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    return
end if

li_RestoredSlashes = loo_ObjectPath.Replace("%2F","/")
li_Success = loo_ObjectPath.Prepend("/")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    return
end if

//  The sidecar must describe the SAME partial file and original object.
//  Save its exact quoted ETag and full Content-Length when starting the download.
loo_Checkpoint = create oleobject
li_rc = loo_Checkpoint.ConnectToNewObject("Chilkat.JsonObject")

loo_Checkpoint.Strict = 1
li_Success = loo_Checkpoint.LoadFile("s3-download.json")
if li_Success <> 1 then
    Write-Debug "Could not load the download checkpoint."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Checkpoint.StringOf("bucket"))
li_SameText4 = loo_TextCompare.ContentsEqual("",1)
if (loo_Checkpoint.JsonTypeOf("bucket") <> 1) OR (li_SameText4 = 1) then
    Write-Debug "Invalid checkpoint field: bucket"
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Checkpoint.StringOf("key"))
li_SameText5 = loo_TextCompare.ContentsEqual("",1)
if (loo_Checkpoint.JsonTypeOf("key") <> 1) OR (li_SameText5 = 1) then
    Write-Debug "Invalid checkpoint field: key"
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Checkpoint.StringOf("region"))
li_SameText6 = loo_TextCompare.ContentsEqual("",1)
if (loo_Checkpoint.JsonTypeOf("region") <> 1) OR (li_SameText6 = 1) then
    Write-Debug "Invalid checkpoint field: region"
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Checkpoint.StringOf("etag"))
li_SameText7 = loo_TextCompare.ContentsEqual("",1)
if (loo_Checkpoint.JsonTypeOf("etag") <> 1) OR (li_SameText7 = 1) then
    Write-Debug "Invalid checkpoint field: etag"
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Checkpoint.StringOf("total_size"))
li_SameText8 = loo_TextCompare.ContentsEqual("",1)
if (loo_Checkpoint.JsonTypeOf("total_size") <> 1) OR (li_SameText8 = 1) then
    Write-Debug "Invalid checkpoint field: total_size"
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Checkpoint.StringOf("bucket"))
li_SameText9 = loo_TextCompare.ContentsEqual(ls_BucketName,1)
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Checkpoint.StringOf("key"))
li_SameText10 = loo_TextCompare.ContentsEqual(ls_ObjectKey,1)
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Checkpoint.StringOf("region"))
li_SameText11 = loo_TextCompare.ContentsEqual(ls_Region,1)
if (li_SameText9 <> 1) OR (li_SameText10 <> 1) OR (li_SameText11 <> 1) then
    Write-Debug "Checkpoint does not match this object."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    return
end if

loo_Files = create oleobject
li_rc = loo_Files.ConnectToNewObject("Chilkat.FileAccess")

ls_LocalSize = loo_Files.FileSizeStr("s3-download.part")
if loo_Files.LastMethodSuccess <> 1 then
    Write-Debug "Could not determine the partial file size."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_LocalSize)
li_SameText12 = loo_TextCompare.ContentsEqual(loo_Checkpoint.StringOf("total_size"),1)
if li_SameText12 = 1 then
    Write-Debug "Local length already matches the checkpoint; verify integrity before publishing."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    return
end if

loo_Range = create oleobject
li_rc = loo_Range.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Range.Append("bytes=")
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    return
end if

li_Success = loo_Range.Append(ls_LocalSize)
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    return
end if

li_Success = loo_Range.Append("-")
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    return
end if

li_Success = loo_Rest.AddHeader("Range",loo_Range.GetAsString())
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    return
end if

li_Success = loo_Rest.AddHeader("If-Match",loo_Checkpoint.StringOf("etag"))
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    return
end if

li_Success = loo_Rest.SendReqNoBody("GET",loo_ObjectPath.GetAsString())
if li_Success <> 1 then
    Write-Debug "Could not send download request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    return
end if

li_StatusCode = loo_Rest.ReadResponseHeader()
if li_StatusCode < 0 then
    Write-Debug "Could not read download headers."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    return
end if

if li_StatusCode <> 206 then
    Write-Debug "Download HTTP status: " + string(li_StatusCode)
    ls_ErrorBody = loo_Rest.ReadRespBodyString()
    if loo_Rest.LastMethodSuccess = 1 then
        li_Success = loo_Xml.LoadXml(ls_ErrorBody)
        loo_TextCompare.Clear()
        loo_TextCompare.SetString(loo_Xml.Tag)
        li_SameText13 = loo_TextCompare.ContentsEqual("Error",1)
        if (li_Success = 1) AND (li_SameText13 = 1) then
            Write-Debug "S3 error code: " + loo_Xml.GetChildContent("Code")
        end if

    end if

    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    return
end if

//  Never append HTTP 200, 412, or 416 to the existing file.
ls_ReturnedEtag = loo_Rest.ResponseHdrByName("ETag")
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_ReturnedEtag)
li_SameText14 = loo_TextCompare.ContentsEqual(loo_Checkpoint.StringOf("etag"),1)
if (loo_Rest.LastMethodSuccess <> 1) OR (li_SameText14 <> 1) then
    Write-Debug "Object validator changed; partial file was not modified."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    return
end if

loo_ContentRange = create oleobject
li_rc = loo_ContentRange.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_ContentRange.Append(loo_Rest.ResponseHdrByName("Content-Range"))
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    destroy loo_ContentRange
    return
end if

loo_ExpectedStart = create oleobject
li_rc = loo_ExpectedStart.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_ExpectedStart.Append("bytes ")
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    destroy loo_ContentRange
    destroy loo_ExpectedStart
    return
end if

li_Success = loo_ExpectedStart.Append(ls_LocalSize)
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    destroy loo_ContentRange
    destroy loo_ExpectedStart
    return
end if

li_Success = loo_ExpectedStart.Append("-")
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    destroy loo_ContentRange
    destroy loo_ExpectedStart
    return
end if

if loo_ContentRange.StartsWith(loo_ExpectedStart.GetAsString(),1) <> 1 then
    Write-Debug "Range start does not match the partial file."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    destroy loo_ContentRange
    destroy loo_ExpectedStart
    return
end if

loo_ExpectedTotal = create oleobject
li_rc = loo_ExpectedTotal.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_ExpectedTotal.Append("/")
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    destroy loo_ContentRange
    destroy loo_ExpectedStart
    destroy loo_ExpectedTotal
    return
end if

li_Success = loo_ExpectedTotal.Append(loo_Checkpoint.StringOf("total_size"))
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    destroy loo_ContentRange
    destroy loo_ExpectedStart
    destroy loo_ExpectedTotal
    return
end if

if loo_ContentRange.EndsWith(loo_ExpectedTotal.GetAsString(),1) <> 1 then
    Write-Debug "Object length changed; partial file was not modified."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    destroy loo_ContentRange
    destroy loo_ExpectedStart
    destroy loo_ExpectedTotal
    return
end if

//  The file must be an unchanged prefix of the object; size alone cannot prove that.
loo_Output = create oleobject
li_rc = loo_Output.ConnectToNewObject("Chilkat.Stream")

loo_Output.SinkFile = "s3-download.part"
loo_Output.SinkFileAppend = 1
li_Success = loo_Rest.ReadRespBodyStream(loo_Output,1)
if li_Success <> 1 then
    Write-Debug "Resume incomplete; retain the partial file and original checkpoint."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    destroy loo_ContentRange
    destroy loo_ExpectedStart
    destroy loo_ExpectedTotal
    destroy loo_Output
    return
end if

ls_FinalSize = loo_Files.FileSizeStr("s3-download.part")
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_FinalSize)
li_SameText15 = loo_TextCompare.ContentsEqual(loo_Checkpoint.StringOf("total_size"),1)
if (loo_Files.LastMethodSuccess <> 1) OR (li_SameText15 <> 1) then
    Write-Debug "Result length does not match the original full object."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_ObjectPath
    destroy loo_Checkpoint
    destroy loo_Files
    destroy loo_Range
    destroy loo_ContentRange
    destroy loo_ExpectedStart
    destroy loo_ExpectedTotal
    destroy loo_Output
    return
end if

Write-Debug "Resumed download complete."


destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_ObjectPath
destroy loo_Checkpoint
destroy loo_Files
destroy loo_Range
destroy loo_ContentRange
destroy loo_ExpectedStart
destroy loo_ExpectedTotal
destroy loo_Output