Zig
Zig
S3 Upload String using AWS Signature Version 2
See more Amazon S3 (new) Examples
Example to upload the contents of a string to the Amazon S3 service, using the older AWS Signature Version 2.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Demonstrates how to use older AWS S3 Signature Version 2 for uploading the contents
// of a string variable to an object in a bucket.
// 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;
// The file is uploaded to the bucket named "chilkat100", which becomes part of the domain:
try rest.connect("chilkat100.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");
// For AWS Signature Version 2, the following two properties need to be set:
auth_aws.setSignatureVersion(2);
// See http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#ConstructingTheCanonicalizedResourceElement
// The bucket is "chilkat100", and the uriPath is "/hamlet_play.xml", which must match
// the URI path passed to the FullRequestString method below.
auth_aws.setCanonicalizedResourceV2("/chilkat100/hamlet_play.xml");
try rest.setAuthAws(auth_aws);
// 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", .{});
}