Sample code for 30+ languages & platforms
Lianja

S3 Resume Download

See more Amazon S3 (new) Examples

Suppose an S3 download of a very large file failed for some reason and you have a partial file on disk. Rather than restart the entire download, you wish to download the remaining portion. This example demonstrates how to finish a previously failed download.

Note: This example requires Chilkat v9.5.0.83 or above.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loRest = createobject("CkRest")

// Connect to the Amazon AWS REST server.
llBTls = .T.
lnPort = 443
llBAutoReconnect = .T.
llSuccess = loRest.Connect("s3.amazonaws.com",lnPort,llBTls,llBAutoReconnect)

// ---------------------------------------------------------------------------
// Important: For buckets created in regions outside us-east-1,
// there are three important changes that need to be made.
// See Working with S3 Buckets in Non-us-east-1 Regions for the details.
// ---------------------------------------------------------------------------

// Provide AWS credentials for the REST call.
loAuthAws = createobject("CkAuthAws")
loAuthAws.AccessKey = "AWS_ACCESS_KEY"
loAuthAws.SecretKey = "AWS_SECRET_KEY"
loAuthAws.ServiceName = "s3"
llSuccess = loRest.SetAuthAws(loAuthAws)

// Set the bucket name via the HOST header.
// In this case, the bucket name is "chilkat100".
loRest.Host = "chilkat100.s3.amazonaws.com"

// We want to continue downloading a file.
// The relative local filepath of our previously partially downoaded file is: qa_output/hamlet.xml
// Let's find out how many bytes are already downloaded.
loFac = createobject("CkFileAccess")
lcLocalFilepath = "qa_output/hamlet.xml"
// Note: The FileSize method returns a signed 32-bit integer.  If the file is potentially larger than 2GB, call FileSizeStr instead to return
// the size of the file as a string, then convert to an integer value.
lnSz = loFac.FileSize(lcLocalFilepath)
if (lnSz > 0) then
    ? "sz = " + str(lnSz)

    // If the sz equals 42375 bytes, then we want to add a Range header that looks like this:
    // Range: bytes=42375-
    loSbRange = createobject("CkStringBuilder")
    loSbRange.Append("bytes=")
    loSbRange.AppendInt(lnSz)
    loSbRange.Append("-")
    loRest.AddHeader("Range",loSbRange.GetAsString())
    ? "Added Range: " + loSbRange.GetAsString()
endif

// Send the request to download the remainder of the file.
llSuccess = loRest.SendReqNoBody("GET","/hamlet.xml")
if (llSuccess <> .T.) then
    ? loRest.LastErrorText
    release loRest
    release loAuthAws
    release loFac
    release loSbRange
    return
endif

// Read the response header.
lnResponseStatusCode = loRest.ReadResponseHeader()
if (lnResponseStatusCode < 0) then
    ? loRest.LastErrorText
    release loRest
    release loAuthAws
    release loFac
    release loSbRange
    return
endif

? "Response status code = " + str(lnResponseStatusCode)

// We expect a 200 or 206 response status if the file data is coming.
// Otherwise, we'll get a string response body with an error message(or no response body).
if ((lnResponseStatusCode = 200) or (lnResponseStatusCode = 206)) then

    loBodyStream = createobject("CkStream")

    // The stream's sink will be a file.
    // We will append to the file..
    loBodyStream.SinkFile = lcLocalFilepath

    // Indicate that we wish to append to the output file.
    // The SinkFileAppend property was added in Chilkat v9.50.83
    loBodyStream.SinkFileAppend = .T.

    // Read the response body to the stream.  Given that we've
    // set the stream's sink to a file, it will stream directly
    // to the file.
    llSuccess = loRest.ReadRespBodyStream(loBodyStream,.T.)
    if (llSuccess <> .T.) then
        ? loRest.LastErrorText
        release loRest
        release loAuthAws
        release loFac
        release loSbRange
        release loBodyStream
        return
    endif

    ? "Successfully downloaded the file."

else
    lcErrResponse = loRest.ReadRespBodyString()
    if (loRest.LastMethodSuccess <> .T.) then
        ? loRest.LastErrorText
    else
        ? lcErrResponse
    endif

endif



release loRest
release loAuthAws
release loFac
release loSbRange
release loBodyStream