Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect to the Amazon AWS REST server.
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("s3.amazonaws.com", port, b_tls, b_auto_reconnect);

    // ---------------------------------------------------------------------------
    // 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.
    const auth_aws = try chilkat.AuthAws.init();
    defer auth_aws.deinit();
    auth_aws.setAccessKey("AWS_ACCESS_KEY");
    auth_aws.setSecretKey("AWS_SECRET_KEY");
    auth_aws.setServiceName("s3");
    try rest.setAuthAws(auth_aws);

    // Set the bucket name via the HOST header.
    // In this case, the bucket name is "chilkat100".
    rest.setHost("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.
    const fac = try chilkat.FileAccess.init();
    defer fac.deinit();
    const local_filepath = "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.
    const sz = fac.fileSize(local_filepath);
    if (sz > 0) {
        std.debug.print("sz = {d}\n", .{sz});

        // If the sz equals 42375 bytes, then we want to add a Range header that looks like this:
        // Range: bytes=42375-
        const sb_range = try chilkat.StringBuilder.init();
        defer sb_range.deinit();
        sb_range.append("bytes=") catch {};
        sb_range.appendInt(sz) catch {};
        sb_range.append("-") catch {};
        rest.addHeader("Range", try sb_range.getAsString(alloc)) catch {};
        std.debug.print("Added Range: {s}\n", .{try sb_range.getAsString(alloc)});
    }

    // Send the request to download the remainder of the file.
    rest.sendReqNoBody("GET", "/hamlet.xml") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Read the response header.
    const response_status_code = rest.readResponseHeader();
    if (response_status_code < 0) {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    }

    std.debug.print("Response status code = {d}\n", .{response_status_code});

    // 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 ((response_status_code == 200) or (response_status_code == 206)) {
        const body_stream = try chilkat.Stream.init();
        defer body_stream.deinit();

        // The stream's sink will be a file.
        // We will append to the file..
        body_stream.setSinkFile(local_filepath);

        // Indicate that we wish to append to the output file.
        // The SinkFileAppend property was added in Chilkat v9.50.83
        body_stream.setSinkFileAppend(true);

        // 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.
        rest.readRespBodyStream(body_stream, true) catch {
            std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
            return;
        };

        std.debug.print("Successfully downloaded the file.\n", .{});
    } else {
        const err_response = rest.readRespBodyString(alloc) catch "";
        if (!rest.getLastMethodSuccess()) {
            std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        } else {
            std.debug.print("{s}\n", .{err_response});
        }
    }
}