Sample code for 30+ languages & platforms
Zig

Fetch S3 Object Metadata

See more Amazon S3 (new) Examples

Demonstrates how to get the metadata for an S3 object using the REST API.

The HEAD operation retrieves metadata from an object without returning the object itself. This operation is useful if you are interested only in an object's metadata. To use HEAD, you must have READ access to the object.

A HEAD request has the same options as a GET operation on an object. The response is identical to the GET response except that there is no response body.

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

    // 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");
    // Make sure the Region agrees with the region in the Connect.
    auth_aws.setRegion("us-west-2");
    try rest.setAuthAws(auth_aws);

    // User-defined metadata are name/value pairs, and are returned in the HTTP response header.
    // Metadata header names begin with "x-amz-meta-" to distinguish them from other HTTP headers.
    // Note that Amazon S3 stores user-defined metadata keys in lowercase.

    // Set the bucket name via the HOST header.
    // In this case, the bucket name is "chilkat.ocean".
    rest.setHost("chilkat.ocean.s3.amazonaws.com");

    // Send the HEAD request.
    rest.sendReqNoBody("HEAD", "/seahorse.jpg") 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});
    if (response_status_code != 200) {
        std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("Object does not exist.\n", .{});
        return;
    }

    // Show the full response header that was received:
    std.debug.print("Response header:\n", .{});
    std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
    std.debug.print("--\n", .{});

    // Here is an example response header:

    //     x-amz-id-2: uS4Flff04M8x5YWajU231TP0ClBL19mMhuyfU5ZVQd6NsUHXVhHK+H3b0sjxY98Fujet1ejhyzk=
    //     x-amz-request-id: 27950009AA8E68AA
    //     Date: Mon, 23 Jan 2017 20:12:58 GMT
    //     Last-Modified: Fri, 20 Jan 2017 00:22:57 GMT
    //     ETag: "a8551f0a5437f43a796fca7623ee9232"
    //     x-amz-meta-species: big-belly seahorse
    //     x-amz-meta-genus: Hippocampus
    //     x-amz-meta-habitat: shallow tropical and temperate waters
    //     Accept-Ranges: bytes
    //     Content-Type: image/jpg
    //     Content-Length: 24388
    //     Server: AmazonS3

    // Examine particular response headers (the object metadata headers..)
    std.debug.print("x-amz-meta-species: {s}\n", .{try rest.responseHdrByName(alloc, "x-amz-meta-species")});
    std.debug.print("x-amz-meta-genus: {s}\n", .{try rest.responseHdrByName(alloc, "x-amz-meta-genus")});
    std.debug.print("x-amz-meta-habitat: {s}\n", .{try rest.responseHdrByName(alloc, "x-amz-meta-habitat")});
    std.debug.print("--\n", .{});

    // It is possible to iterate over the header fields to find all x-amz-meta* headers
    var i: i32 = 0;
    const num_headers = rest.getNumResponseHeaders();
    const sb_name = try chilkat.StringBuilder.init();
    defer sb_name.deinit();
    while (i < num_headers) {
        sb_name.setString(try rest.responseHdrName(alloc, i)) catch {};
        if (sb_name.startsWith("x-amz-meta", false)) {
            std.debug.print("{s}: {s}\n", .{ try sb_name.getAsString(alloc), try rest.responseHdrValue(alloc, i) });
        }

        i = i + 1;
    }

    // The output:

    //     x-amz-meta-species: big-belly seahorse
    //     x-amz-meta-genus: Hippocampus
    //     x-amz-meta-habitat: shallow tropical and temperate waters
}