Zig
Zig
S3 Find Bucket Region
See more Amazon S3 (new) Examples
Demonstrates how to find out the region of an S3 bucket.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This 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.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");
try rest.setAuthAws(auth_aws);
// The bucket name we want to know about is set in the HOST header.
// In this example, we want to find the region for the "chilkat.qa" bucket.
rest.setHost("chilkat.qa.s3.amazonaws.com");
// Send the GET request to query the bucket location.
const str_result = rest.fullRequestNoBody(alloc, "GET", "/?location") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const response_status_code = rest.getResponseStatusCode();
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("{s}\n", .{str_result});
std.debug.print("Failed.\n", .{});
return;
}
std.debug.print("{s}\n", .{str_result});
// The result contains XML like this:
// <?xml version="1.0" encoding="UTF-8"?>
// <LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">us-west-2</LocationConstraint>
// Get the "us-west-2" from the XML
const xml = try chilkat.Xml.init();
defer xml.deinit();
try xml.loadXml(str_result);
std.debug.print("region = {s}\n", .{try xml.getContent(alloc)});
}