Sample code for 30+ languages & platforms
Zig

Glacier Delete All Archives in a Vault

See more Amazon Glacier Examples

Iterates over a vault inventory retrieval and deletes each archive.

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 in the desired region.
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("glacier.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect);

    // Provide AWS credentials.
    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("glacier");
    auth_aws.setRegion("us-west-2");

    try rest.setAuthAws(auth_aws);

    // --------------------------------------------------------------------------
    // Note: The above REST connection and setup of the AWS credentials
    // can be done once.  After connecting, any number of REST calls can be made.
    // The "auto reconnect" property passed to rest.Connect indicates that if
    // the connection is lost, a REST method call will automatically reconnect
    // if needed.
    // --------------------------------------------------------------------------

    // For more information, see Glacier Retrieve Job Output Reference Documentation
    //
    rest.addHeader("x-amz-glacier-version", "2012-06-01") catch {};

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    rest.fullRequestNoBodySb("GET", "/AWS_ACCOUNT_ID/vaults/chilkat/jobs/JOB_ID/output", sb_response_body) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    var resp_status_code: i32 = rest.getResponseStatusCode();
    if (resp_status_code >= 400) {
        std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("Response Body:\n", .{});
        std.debug.print("{s}\n", .{try sb_response_body.getAsString(alloc)});
        return;
    }

    // Success is indicated by a 200 response status.
    std.debug.print("response status code = {d}\n", .{resp_status_code});

    // The inventory retrieval job output is in CSV format.
    std.debug.print("job output:\n", .{});
    std.debug.print("{s}\n", .{try sb_response_body.getAsString(alloc)});
    std.debug.print("--\n", .{});

    // The CSV has these fields:  ArchiveId,ArchiveDescription,CreationDate,Size,SHA256TreeHash
    const csv = try chilkat.Csv.init();
    defer csv.deinit();
    csv.setHasColumnNames(true);
    csv.loadFromString(try sb_response_body.getAsString(alloc)) catch {};

    // Iterate over the results and delete each archive from the vault.
    var i: i32 = 0;
    const num_rows = csv.getNumRows();
    while (i < num_rows) {
        const archive_id = try csv.getCellByName(alloc, i, "ArchiveId");
        std.debug.print("Deleting ArchiveId: {s}\n", .{archive_id});
        rest.addPathParam("ArchiveID", archive_id) catch {};
        rest.fullRequestNoBodySb("DELETE", "/AWS_ACCOUNT_ID/vaults/chilkat/archives/ArchiveID", sb_response_body) catch {
            std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
            return;
        };

        resp_status_code = rest.getResponseStatusCode();

        // Success is indicated by a 204 response status with an empty response body.
        // A 404 resonse indicates the archive was likely already deleted.
        std.debug.print("response status code = {d}\n", .{resp_status_code});

        i = i + 1;
    }
}