Sample code for 30+ languages & platforms
Zig

S3 Add Tags to an Object

See more Amazon S3 (new) Examples

Demonstrates how to add one or more tags to an S3 object.

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.
    // (for us-east-1, we use "s3.amazonaws.com", but for another region, such as us-west-2, we would use "s3-us-west-2.amazonaws.com")
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("s3.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("s3");
    auth_aws.setRegion("us-east-1");

    rest.setAuthAws(auth_aws) catch {};

    // Set the bucket name via the HOST header.
    // In this case, the bucket name is "chilkat100".
    // Note that the Host header should use "bucketName.s3.amazonaws.com", not "bucketName.s3-us-east-1.amazonaws.com"
    // The same applies to aother regions.  The Host header should simply be <bucketName>.s3.amazonaws.com regardless of the region.
    rest.setHost("chilkat100.s3.amazonaws.com");

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("Tagging");
    xml.updateChildContent("TagSet|Tag|Key", "plant");
    xml.updateChildContent("TagSet|Tag|Value", "chili pepper");

    const sb_request_body = try chilkat.StringBuilder.init();
    defer sb_request_body.deinit();
    xml.getXmlSb(sb_request_body) catch {};

    // It is important to add the terminating "=" after the "?tagging".
    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    rest.fullRequestSb("PUT", "/chiliPepper.gif?tagging=", sb_request_body, sb_response) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

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

    // When successful, the S3 Storage service will respond with a 200 response code,
    // with an XML body.
    if (rest.getResponseStatusCode() != 200) {
        // Examine the request/response to see what happened.
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response body: {s}\n", .{try sb_response.getAsString(alloc)});
        std.debug.print("---\n", .{});
        std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
        std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
    }

    std.debug.print("{s}\n", .{try sb_response.getAsString(alloc)});
    std.debug.print("Success.\n", .{});
}