Sample code for 30+ languages & platforms
Zig

S3 Rename File

See more Amazon S3 (new) Examples

Demonstrates how to rename a file on Amazon S3.

Renaming an object (file) in AWS S3 using the AWS S3 API involves copying the object to a new location with the desired name and then deleting the original 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.
    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.
    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");

    // This particular bucket is in the Oregon region, as opposed to the US Standard,
    // therefore the Region must be set appropriately.
    // Also note that we connected to "s3-us-west-2.amazonaws.com".
    auth_aws.setRegion("us-west-2");

    rest.setAuthAws(auth_aws) catch {};

    // Set the bucket name via the HOST header.
    // In this case, the bucket name is "chilkat.qa".
    // Note that the Host header should use "bucketName.s3.amazonaws.com", not "bucketName.s3-us-west-2.amazonaws.com"
    rest.setHost("chilkat.qa.s3.amazonaws.com");

    // Copy /images/sea_creatures/starfish.jpg to /images/sea_creatures/starfish3.jpg
    // Notice here that the x-amz-copy-source includes the bucket in the source path,
    // but the target path passed to FullRequestNoBody does NOT contain the bucket (because the bucket
    // was already specified in the Host header).
    rest.addHeader("x-amz-copy-source", "/chilkat.qa/images/sea_creatures/starfish.jpg") catch {};

    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    rest.fullRequestNoBodySb("PUT", "/images/sea_creatures/starfish3.jpg", sb_response) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // When successful, the S3 Storage service will respond with a 200 or 204 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("Copy Successful.\n", .{});

    // --------------------------------------
    // Now we can delete the original file..
    // --------------------------------------

    // We no longer want to send the x-amz-copy-source header.  Remove it.
    rest.removeHeader("x-amz-copy-source") catch {};

    rest.fullRequestNoBodySb("DELETE", "/images/sea_creatures/starfish.jpg", sb_response) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // If successfully deleted, the response status code is 204 and the response body text will be empty.
    const status_code = rest.getResponseStatusCode();
    std.debug.print("Response status code = {d}\n", .{status_code});
    std.debug.print("Response text = \n", .{});
    std.debug.print("{s}\n", .{try sb_response.getAsString(alloc)});
}