Zig
Zig
Create a Google Cloud Storage Bucket
See more Google Cloud Storage Examples
Demonstrates how to create a Google Cloud Storage 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 example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example uses a previously obtained access token having permission for the
// scope "https://www.googleapis.com/auth/cloud-platform"
// In this example, Get Google Cloud Storage OAuth2 Access Token,
// the service account access token was saved to a text file. This example fetches the access token from the file..
const sb_token = try chilkat.StringBuilder.init();
defer sb_token.deinit();
sb_token.loadFile("qa_data/tokens/googleCloudStorageAccessToken.txt", "utf-8") catch {};
// ---- Step 1
// Create a JSON that contains the following information, replacing [VALUES_IN_BRACKETS] with the appropriate values:
// {
// "name": "[BUCKET_NAME]",
// "location": "[BUCKET_LOCATION]",
// "storageClass": "[STORAGE_CLASS]"
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("name", "chilkat-ocean") catch {};
json.updateString("location", "us") catch {};
json.updateString("storageClass", "multi_regional") catch {};
// ---- Step 2
// Send a POST equivalent to this curl command
// curl -X POST --data-binary @[JSON_FILE_NAME].json \
// -H "Authorization: Bearer [OAUTH2_TOKEN]" \
// -H "Content-Type: application/json" \
// "https://www.googleapis.com/storage/v1/b?project=[PROJECT_ID]"
const http = try chilkat.Http.init();
defer http.deinit();
http.setAuthToken(try sb_token.getAsString(alloc));
http.setUrlVar("project_id", "chilkat-176611") catch {};
const url = "https://www.googleapis.com/storage/v1/b?project={$project_id}";
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", url, json, "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const response_code = resp.getStatusCode();
if (response_code == 401) {
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
std.debug.print("If invalid credentials, then it is likely the access token expired.\n", .{});
std.debug.print("Your app should automatically fetch a new access token and re-try.\n", .{});
return;
}
std.debug.print("Response code: {d}\n", .{response_code});
std.debug.print("Response body\n", .{});
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
// A successful response looks like this:
// Response code: 200
// Response body
// {
// "kind": "storage#bucket",
// "id": "chilkat-ocean",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean",
// "projectNumber": "933037264567",
// "name": "chilkat-ocean",
// "timeCreated": "2017-08-13T12:27:10.811Z",
// "updated": "2017-08-13T12:27:10.811Z",
// "metageneration": "1",
// "location": "US",
// "storageClass": "MULTI_REGIONAL",
// "etag": "CAE="
// }
//
// Parse the JSON response:
const json_resp = try chilkat.JsonObject.init();
defer json_resp.deinit();
json_resp.load(try resp.getBodyStr(alloc)) catch {};
std.debug.print("name: {s}\n", .{try json_resp.stringOf(alloc, "name")});
std.debug.print("timeCreated: {s}\n", .{try json_resp.stringOf(alloc, "timeCreated")});
// Parse the date/time created.
const dt = try chilkat.DateTime.init();
defer dt.deinit();
dt.setFromTimestamp(try json_resp.stringOf(alloc, "timeCreated")) catch {};
// ...
}