Zig
Zig
Upload Binary File from Memory
See more Google Drive Examples
This example demonstrates how to upload a binary file from memory. It uploads the file to a subdirectory of our choosing.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
// Google Drive scope.
const g_auth = try chilkat.AuthGoogle.init();
defer g_auth.deinit();
g_auth.setAccessToken("GOOGLE-DRIVE-ACCESS-TOKEN");
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect using TLS.
const b_auto_reconnect = true;
try rest.connect("www.googleapis.com", 443, true, b_auto_reconnect);
// Provide the authentication credentials (i.e. the access token)
rest.setAuthGoogle(g_auth) catch {};
// -------------------------------------------------------------------------
// A multipart upload to Google Drive needs a multipart/related Content-Type
rest.addHeader("Content-Type", "multipart/related") catch {};
// Specify each part of the request.
// The 1st part is JSON with information about the file.
rest.setPartSelector("1");
rest.addHeader("Content-Type", "application/json; charset=UTF-8") catch {};
// Construct the JSON that will contain the metadata about the file data to be uploaded...
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.appendString("name", "hedgehogs.jpg") catch {};
json.appendString("description", "A cute photo of two hedgehogs.") catch {};
json.appendString("mimeType", "image/jpeg") catch {};
// To place the file in a folder, we must add a parents[] array to the JSON
// and add the folder ID.
// In a previous example (see Build Local Metadata Cache
// we built a local cache to make it easy to lookup file IDs given a file path.
// Let's find the file ID for the folder "testFolder/abc/123"
const gd_cache = try chilkat.Cache.init();
defer gd_cache.deinit();
gd_cache.setLevel(0);
gd_cache.addRoot("C:/ckCache/googleDrive");
const folder_id = gd_cache.fetchText(alloc, "testFolder/abc/123") catch {
std.debug.print("Filepath not found in cache.\n", .{});
return;
};
const parents = try chilkat.JsonArray.init();
defer parents.deinit();
json.appendArray2("parents", parents) catch {};
parents.addStringAt(-1, folder_id) catch {};
rest.setMultipartBodyString(try json.emit(alloc)) catch {};
// The 2nd part is the file content, which will contain the binary image data.
rest.setPartSelector("2");
rest.addHeader("Content-Type", "image/jpeg") catch {};
const fac = try chilkat.FileAccess.init();
defer fac.deinit();
// Read a JPG file from the local filesystem.
ERROR: Method not in the Zig package: FileAccess.ReadEntireFile
ERROR: Method not in the Zig package: FileAccess.ReadEntireFile
jpg_bytes = fac.ERROR();
// Add the bytes to our upload
ERROR: Method not in the Zig package: Rest.SetMultipartBodyBinary
_ = rest.ERROR();
const json_response = rest.fullRequestMultipart(alloc, "POST", "/upload/drive/v3/files?uploadType=multipart") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// A successful response will have a status code equal to 200.
if (rest.getResponseStatusCode() != 200) {
std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("response JSON: {s}\n", .{json_response});
return;
}
// Show the JSON response.
json.load(json_response) catch {};
// Show the full JSON response.
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// A successful response looks like this:
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYoldmJ0Z3ZqT2x5MFk",
// "name": "hedgehogs.jpg",
// "mimeType": "image/jpeg"
// }
// Get the fileId:
std.debug.print("fileId: {s}\n", .{try json.stringOf(alloc, "id")});
}