Sample code for 30+ languages & platforms
Zig

S3 List Buckets (using Chilkat REST)

See more Amazon S3 (new) Examples

Demonstrates how to fetch a list of S3 buckets.

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

    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    rest.fullRequestNoBodySb("GET", "/", 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 name: [:0]const u8 = "";
    var creation_date: [:0]const u8 = "";

    var i: i32 = 0;
    const count_i = xml.numChildrenHavingTag("Buckets|Bucket");
    while (i < count_i) {
        xml.setI(i);
        name = try xml.getChildContent(alloc, "Buckets|Bucket[i]|Name");
        creation_date = try xml.getChildContent(alloc, "Buckets|Bucket[i]|CreationDate");
        std.debug.print("{s}\n", .{name});
        std.debug.print("{s}\n", .{creation_date});
        std.debug.print("--\n", .{});
        i = i + 1;
    }
}