Sample code for 30+ languages & platforms
Zig

S3 List Bucket Objects (Bucket-in-Path)

See more Amazon S3 (new) Examples

Demonstrates how to fetch a list of objects in an S3 bucket, but using the bucket-in-path request format instead of putting the bucket name in the HOST header.

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);

    // The bucket name is "chilkat100"
    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    rest.fullRequestNoBodySb("GET", "/chilkat100", sb_response) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const status_code = rest.getResponseStatusCode();
    std.debug.print("Response status code = {d}\n", .{status_code});

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadSb(sb_response, true) catch {};
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    if (status_code != 200) {
        std.debug.print("Failed.  See error information in the XML.\n", .{});
        return;
    }

    // Use this online tool to generate code from sample XML:
    // Generate Code to Create XML

    var key: [:0]const u8 = "";
    var last_modified: [:0]const u8 = "";
    var e_tag: [:0]const u8 = "";
    var size_decimal_str: [:0]const u8 = "";
    var id: [:0]const u8 = "";
    var display_name: [:0]const u8 = "";
    var storage_class: [:0]const u8 = "";

    _ = try xml.getChildContent(alloc, "Name");
    _ = xml.getChildIntValue("MaxKeys");
    _ = try xml.getChildContent(alloc, "IsTruncated");
    var i: i32 = 0;
    const count_i = xml.numChildrenHavingTag("Contents");
    while (i < count_i) {
        xml.setI(i);
        key = try xml.getChildContent(alloc, "Contents[i]|Key");
        last_modified = try xml.getChildContent(alloc, "Contents[i]|LastModified");
        e_tag = try xml.getChildContent(alloc, "Contents[i]|ETag");
        size_decimal_str = try xml.getChildContent(alloc, "Contents[i]|Size");
        id = try xml.getChildContent(alloc, "Contents[i]|Owner|ID");
        display_name = try xml.getChildContent(alloc, "Contents[i]|Owner|DisplayName");
        storage_class = try xml.getChildContent(alloc, "Contents[i]|StorageClass");
        i = i + 1;
    }

    std.debug.print("Success.\n", .{});
}