Zig
Zig
Google Cloud Storage List Bucket Objects
See more Google Cloud Storage Examples
List the objects in 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 {};
const http = try chilkat.Http.init();
defer http.deinit();
http.setAuthToken(try sb_token.getAsString(alloc));
// For more info see Cloud Storage Documentation - Objects: list
//
try http.setUrlVar("bucket", "chilkat-ocean");
try http.setUrlVar("PROJECT_NAME", "ChilkatCloud");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpNoBody("GET", "https://www.googleapis.com/storage/v1/b/{$bucket}/o?project={$ChilkatCloud}", 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#objects",
// "items": [
// {
// "kind": "storage#object",
// "id": "chilkat-ocean/file with space char.jpg/1555776690752366",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg",
// "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg?generation=1555776690752366&alt=media",
// "name": "file with space char.jpg",
// "bucket": "chilkat-ocean",
// "generation": "1555776690752366",
// "metageneration": "1",
// "contentType": "image/jpeg",
// "storageClass": "MULTI_REGIONAL",
// "size": "6229",
// "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
// "crc32c": "9RjgwQ==",
// "etag": "CO6WguiH3+ECEAE=",
// "timeCreated": "2019-04-20T16:11:30.752Z",
// "updated": "2019-04-20T16:11:30.752Z",
// "timeStorageClassUpdated": "2019-04-20T16:11:30.752Z"
// },
// {
// "kind": "storage#object",
//
// "id": "chilkat-ocean/penguins.jpg/1555775749593296",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/penguins.jpg",
// "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/penguins.jpg?generation=1555775749593296&alt=media",
// "name": "penguins.jpg",
// "bucket": "chilkat-ocean",
// "generation": "1555775749593296",
// "metageneration": "1",
// "contentType": "image/jpeg",
// "storageClass": "MULTI_REGIONAL",
// "size": "777835",
// "md5Hash": "nTd7EM53jEk4s8fixjoimg==",
// "crc32c": "ixxYVw==",
// "etag": "CNCxnqeE3+ECEAE=",
// "timeCreated": "2019-04-20T15:55:49.593Z",
// "updated": "2019-04-20T15:55:49.593Z",
// "timeStorageClassUpdated": "2019-04-20T15:55:49.593Z"
// },
// {
// "kind": "storage#object",
// "id": "chilkat-ocean/starfish.jpg/1628958192639968",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/starfish.jpg",
// "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/starfish.jpg?generation=1628958192639968&alt=media",
// "name": "starfish.jpg",
// "bucket": "chilkat-ocean",
// "generation": "1628958192639968",
// "metageneration": "1",
// "contentType": "image/jpeg",
// "storageClass": "MULTI_REGIONAL",
// "size": "6229",
// "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
// "crc32c": "9RjgwQ==",
// "etag": "COC/tJP2sPICEAE=",
// "timeCreated": "2021-08-14T16:23:12.660Z",
// "updated": "2021-08-14T16:23:12.660Z",
// "timeStorageClassUpdated": "2021-08-14T16:23:12.660Z"
// }
// ]
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var id: [:0]const u8 = "";
var self_link: [:0]const u8 = "";
var media_link: [:0]const u8 = "";
var name: [:0]const u8 = "";
var bucket: [:0]const u8 = "";
var generation: [:0]const u8 = "";
var metageneration: [:0]const u8 = "";
var content_type: [:0]const u8 = "";
var storage_class: [:0]const u8 = "";
var size: [:0]const u8 = "";
var md5_hash: [:0]const u8 = "";
var crc32c: [:0]const u8 = "";
var etag: [:0]const u8 = "";
var time_created: [:0]const u8 = "";
var updated: [:0]const u8 = "";
var time_storage_class_updated: [:0]const u8 = "";
var kind: [:0]const u8 = try json.stringOf(alloc, "kind");
var i: i32 = 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");
media_link = try json.stringOf(alloc, "items[i].mediaLink");
name = try json.stringOf(alloc, "items[i].name");
bucket = try json.stringOf(alloc, "items[i].bucket");
generation = try json.stringOf(alloc, "items[i].generation");
metageneration = try json.stringOf(alloc, "items[i].metageneration");
content_type = try json.stringOf(alloc, "items[i].contentType");
storage_class = try json.stringOf(alloc, "items[i].storageClass");
size = try json.stringOf(alloc, "items[i].size");
md5_hash = try json.stringOf(alloc, "items[i].md5Hash");
crc32c = try json.stringOf(alloc, "items[i].crc32c");
etag = try json.stringOf(alloc, "items[i].etag");
time_created = try json.stringOf(alloc, "items[i].timeCreated");
updated = try json.stringOf(alloc, "items[i].updated");
time_storage_class_updated = try json.stringOf(alloc, "items[i].timeStorageClassUpdated");
i = i + 1;
}
}