Zig
Zig
Create S3 Bucket in a Region
See more Amazon S3 Examples
Demonstrates how to create an S3 bucket in a specified region. This example will create a bucket in the eu-west-2 region.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 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);
// 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);
// We'll send a PUT request having an XML body such as this:
// <CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
// <LocationConstraint>BucketRegion</LocationConstraint>
// </CreateBucketConfiguration>
// Create the XML body specifying the region as "eu-west-2"
const bucket_region = "eu-west-2";
const sb_bucket_region = try chilkat.StringBuilder.init();
defer sb_bucket_region.deinit();
sb_bucket_region.append(bucket_region) catch {};
// We only need to specify the LocationConstraint if the bucket's region is NOT us-east-1
const xml = try chilkat.Xml.init();
defer xml.deinit();
if (!sb_bucket_region.contentsEqual("us-east-1", true)) {
xml.setTag("CreateBucketConfiguration");
xml.addAttribute("xmlns", "http://s3.amazonaws.com/doc/2006-03-01/") catch {};
xml.updateChildContent("LocationConstraint", "eu-west-2");
}
// --------------------------------------------------------------
// IMPORTANT: To create a bucket in the default us-east-1 region,
// do not add the LocationConstraint. Adding a LocationConstraint of "us-east-1"
// causes an error "The specified location-constraint is not valid."
// By default, the bucket is created in us-east-1 by sending a PUT with an empty body.
// --------------------------------------------------------------
// Set the bucket name via the HOST header.
// In this case, the bucket name is "chilkateuwest2".
rest.setHost("chilkateuwest2.s3.amazonaws.com");
// Make the call to create the bucket.
var response_str: [:0]const u8 = "";
if (!sb_bucket_region.contentsEqual("us-east-1", true)) {
response_str = try rest.fullRequestString(alloc, "PUT", "/", try xml.getXml(alloc));
} else {
// If the bucket is to be created in the us-east-1 region (the default region)
// just send a PUT with no body.
response_str = try rest.fullRequestNoBody(alloc, "PUT", "/");
}
if (!rest.getLastMethodSuccess()) {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
}
if (rest.getResponseStatusCode() != 200) {
std.debug.print("status code = {d}\n", .{rest.getResponseStatusCode()});
const response_xml = try chilkat.Xml.init();
defer response_xml.deinit();
response_xml.loadXml(response_str) catch {};
std.debug.print("{s}\n", .{try response_xml.getXml(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
std.debug.print("Bucket in the eu-west-2 region created.\n", .{});
}