Sample code for 30+ languages & platforms
Zig

REST Upload String

See more Amazon S3 (new) Examples

Example to upload the contents of a string to the Amazon S3 service.

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

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

    // Load a text file into memory.
    const fac = try chilkat.FileAccess.init();
    defer fac.deinit();
    const file_contents = fac.readEntireTextFile(alloc, "qa_data/xml/hamlet.xml", "utf-8") catch {
        std.debug.print("{s}\n", .{try fac.getLastErrorText(alloc)});
        return;
    };

    // To send the file in gzip or deflate compressed format, set the Content-Encoding request
    // header to "gzip" or "deflate".  (this is optional)
    try rest.addHeader("Content-Encoding", "gzip");

    // Indicate the Content-Type of our upload.  (This is optional)
    try rest.addHeader("Content-Type", "text/xml");

    // We can add an "Expect: 100-continue" header so that if the request is rejected
    // by the server immediately after receiving the request header, it can respond
    // and the client (Chilkat) can avoid sending the file data.
    // (this is optional)
    try rest.addHeader("Expect", "100-continue");

    // Upload the file to Amazon S3.
    const response_body_str = rest.fullRequestString(alloc, "PUT", "/hamlet_play.xml", file_contents) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Did we get a 200 response indicating success?
    const status_code = rest.getResponseStatusCode();
    if (status_code != 200) {
        std.debug.print("Error response: {s}\n", .{response_body_str});
        std.debug.print("Status code: {d}, Status text: {s}\n", .{ status_code, try rest.getResponseStatusText(alloc) });
        return;
    }

    std.debug.print("File successfully uploaded.\n", .{});
}