Zig
Zig
Amazon Glacier Set Vault Access Policy
See more Amazon Glacier Examples
Demonstrates how to set an access policy for a vault (will overwrite the existing policy).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 in the desired region.
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
try rest.connect("glacier.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("glacier");
auth_aws.setRegion("us-west-2");
try rest.setAuthAws(auth_aws);
// --------------------------------------------------------------------------
// Note: The above REST connection and setup of the AWS credentials
// can be done once. After connecting, any number of REST calls can be made.
// The "auto reconnect" property passed to rest.Connect indicates that if
// the connection is lost, a REST method call will automatically reconnect
// if needed.
// --------------------------------------------------------------------------
// For more information, see Glacier Set Vault Access Policy Reference Documentation
//
rest.addHeader("x-amz-glacier-version", "2012-06-01") catch {};
// Create the following JSON
// Use this online tool to generate the code from sample JSON:
// Generate Code to Create JSON
// {
// "Version": "2012-10-17",
// "Statement": [
// {
// "Sid": "Define-owner-access-rights",
// "Effect": "Allow",
// "Principal": {
// "AWS": "arn:aws:iam::AWS_ACCOUNT_ID:root"
// },
// "Action": "glacier:DeleteArchive",
// "Resource": "arn:aws:glacier:us-west-2:AWS_ACCOUNT_ID:vaults/chilkat"
// }
// ]
// }
const json_policy = try chilkat.JsonObject.init();
defer json_policy.deinit();
json_policy.updateString("Version", "2012-10-17") catch {};
json_policy.updateString("Statement[0].Sid", "Define-owner-access-rights") catch {};
json_policy.updateString("Statement[0].Effect", "Allow") catch {};
json_policy.updateString("Statement[0].Principal.AWS", "arn:aws:iam::AWS_ACCOUNT_ID:root") catch {};
json_policy.updateString("Statement[0].Action", "glacier:DeleteArchive") catch {};
// Notice here the name of the vault: "chilkat". Change it to your vault name. Also use your actual AWS account ID.
json_policy.updateString("Statement[0].Resource", "arn:aws:glacier:us-west-2:AWS_ACCOUNT_ID:vaults/chilkat") catch {};
// Wrap the above JSON in this JSON:
// {
// "Policy": "{ ... the above JSON ... }"
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("Policy", try json_policy.emit(alloc)) catch {};
const sb_request_body = try chilkat.StringBuilder.init();
defer sb_request_body.deinit();
json.emitSb(sb_request_body) catch {};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
rest.fullRequestSb("PUT", "/AWS_ACCOUNT_ID/vaults/chilkat/access-policy", sb_request_body, sb_response_body) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const resp_status_code = rest.getResponseStatusCode();
if (resp_status_code >= 400) {
std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
std.debug.print("Response Header:\n", .{});
std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try sb_response_body.getAsString(alloc)});
return;
}
// Success is indicated by a 204 response status with an empty response body.
std.debug.print("response status code = {d}\n", .{resp_status_code});
}