Sample code for 30+ languages & platforms
Zig

Initiate Resumable Upload Session

See more Google Cloud Storage Examples

Initiate a Google Cloud Storage resumable upload session..

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    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 json_meta_data = try chilkat.JsonObject.init();
    defer json_meta_data.deinit();
    json_meta_data.updateString("contentType", "image/jpeg") catch {};

    // Adds the "Authorization: Bearer <access_token>" header..
    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    http.setUrlVar("bucket_name", "chilkat-bucket-b") catch {};
    http.setUrlVar("object_name", "penguins2.jpg") catch {};
    const url = "https://storage.googleapis.com/upload/storage/v1/b/{$bucket_name}/o?uploadType=resumable&name={$object_name}";
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", url, json_meta_data, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const status_code = resp.getStatusCode();
    std.debug.print("response status code = {d}\n", .{status_code});

    var session_url: [:0]const u8 = "";

    if (status_code != 200) {
        std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
    } else {
        // The session URL will be used to upload the file in chunks, in subsequent HTTP POSTs...
        session_url = try resp.getHeaderField(alloc, "Location");
        std.debug.print("Session URL = {s}\n", .{session_url});
    }
}