Zig
Zig
Google Cloud Storage List Buckets
See more Google Cloud Storage Examples
Demonstrates how to retrieve a list of buckets for a given project.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"
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
json_token.loadFile("qa_data/tokens/googleCloudStorage.json") catch {
std.debug.print("{s}\n", .{try json_token.getLastErrorText(alloc)});
return;
};
const http = try chilkat.Http.init();
defer http.deinit();
http.setAuthToken(try json_token.stringOf(alloc, "access_token"));
// For more info see Cloud Storage Documentation - Buckets: list
//
try http.setUrlVar("PROJECT_ID", "chilkattest-1050");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpNoBody("GET", "https://www.googleapis.com/storage/v1/b?project={$PROJECT_ID}", 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", .{});
const json = try chilkat.JsonObject.init();
defer json.deinit();
try json.load(try resp.getBodyStr(alloc));
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// A response code = 200 indicates success, and the response body contains JSON such as this:
// {
// "kind": "storage#buckets",
// "items": [
// {
// "kind": "storage#bucket",
// "id": "chilkat-bucket",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket",
// "projectNumber": "5332332985",
// "name": "chilkat-bucket",
// "timeCreated": "2018-10-23T00:04:44.507Z",
// "updated": "2018-10-23T00:04:44.507Z",
// "metageneration": "1",
// "iamConfiguration": {
// "bucketPolicyOnly": {
// "enabled": false
// }
// },
// "location": "US",
// "storageClass": "MULTI_REGIONAL",
// "etag": "CAE="
// },
// {
// "kind": "storage#bucket",
// "id": "chilkat-images",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-images",
// "projectNumber": "5332332985",
// "name": "chilkat-images",
// "timeCreated": "2018-10-23T11:24:43.000Z",
// "updated": "2018-10-23T11:24:43.000Z",
// "metageneration": "1",
// "iamConfiguration": {
// "bucketPolicyOnly": {
// "enabled": false
// }
// },
// "location": "US",
// "storageClass": "MULTI_REGIONAL",
// "etag": "CAE="
// }
// ]
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var kind: [:0]const u8 = "";
var i: i32 = 0;
var id: [:0]const u8 = "";
var self_link: [:0]const u8 = "";
var project_number: [:0]const u8 = "";
var name: [:0]const u8 = "";
var time_created: [:0]const u8 = "";
var updated: [:0]const u8 = "";
var metageneration: [:0]const u8 = "";
var iam_configuration_bucket_policy_only_enabled: bool = false;
var location: [:0]const u8 = "";
var storage_class: [:0]const u8 = "";
var etag: [:0]const u8 = "";
kind = try json.stringOf(alloc, "kind");
i = 0;
const count_i = json.sizeOfArray("items");
while (i < count_i) {
json.setI(i);
kind = try json.stringOf(alloc, "items[i].kind");
id = try json.stringOf(alloc, "items[i].id");
self_link = try json.stringOf(alloc, "items[i].selfLink");
project_number = try json.stringOf(alloc, "items[i].projectNumber");
name = try json.stringOf(alloc, "items[i].name");
time_created = try json.stringOf(alloc, "items[i].timeCreated");
updated = try json.stringOf(alloc, "items[i].updated");
metageneration = try json.stringOf(alloc, "items[i].metageneration");
iam_configuration_bucket_policy_only_enabled = json.boolOf("items[i].iamConfiguration.bucketPolicyOnly.enabled");
location = try json.stringOf(alloc, "items[i].location");
storage_class = try json.stringOf(alloc, "items[i].storageClass");
etag = try json.stringOf(alloc, "items[i].etag");
i = i + 1;
}
}