Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.7.0+

Box: Upload a Large File in Parts and Commit the Session

See more Box Examples

Run the reusable setup once per HttpCurl object, then select names and execute the operation. The setup is repeated here only to make this published example independently runnable. Resolve documents by name, then perform the explicit chunked-upload session, part, and commit loop. Supports 20 MB through 2 GB using int. Saves recovery information and honors bounded commit Retry-After.

See the Box API guide for setup and the complete workflow.

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();

    var success: bool = false;

    // Requires Chilkat 11.7.0 or later and a previously unlocked Chilkat bundle.
    // See Global Unlock Sample.
    // Guide: Box API Guide.

    success = false;

    // BEGIN ONE-TIME SETUP
    // This block is repeated so each published example can run on its own.
    // In an application, run the setup example once and pass the SAME curl object
    // to operation code below. Do not recreate it for every operation.
    // See Reusable Box HttpCurl Setup.
    const curl = try chilkat.HttpCurl.init();
    defer curl.deinit();
    curl.setVar("access_token", "BOX_ACCESS_TOKEN");
    curl.setVar("parent_name", "/");
    // Box's fixed root ID is an implementation detail; callers supply names.
    curl.setVar("parent_id", "0");
    const setup_command = try chilkat.StringBuilder.init();
    defer setup_command.deinit();
    var setup_replacements: i32 = 0;

    // Register findParent; registration sends no request.
    setup_command.setString("curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"") catch {};
    setup_replacements = setup_command.replace("ACCESS_TOKEN", try curl.getVar(alloc, "access_token"));
    success = if (curl.addFunction("findParent", try setup_command.getAsString(alloc))) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    // Register findFolder; registration sends no request.
    setup_command.setString("curl -X GET \"https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"") catch {};
    setup_replacements = setup_command.replace("ACCESS_TOKEN", try curl.getVar(alloc, "access_token"));
    success = if (curl.addFunction("findFolder", try setup_command.getAsString(alloc))) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    // Register findFile; registration sends no request.
    setup_command.setString("curl -X GET \"https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"") catch {};
    setup_replacements = setup_command.replace("ACCESS_TOKEN", try curl.getVar(alloc, "access_token"));
    success = if (curl.addFunction("findFile", try setup_command.getAsString(alloc))) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    // Register findDestination; registration sends no request.
    setup_command.setString("curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"") catch {};
    setup_replacements = setup_command.replace("ACCESS_TOKEN", try curl.getVar(alloc, "access_token"));
    success = if (curl.addFunction("findDestination", try setup_command.getAsString(alloc))) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    // Register getFileDetails; registration sends no request.
    setup_command.setString("curl -X GET \"https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version\" -H \"Authorization: Bearer ACCESS_TOKEN\"") catch {};
    setup_replacements = setup_command.replace("ACCESS_TOKEN", try curl.getVar(alloc, "access_token"));
    success = if (curl.addFunction("getFileDetails", try setup_command.getAsString(alloc))) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    // Register getEnterprise; registration sends no request.
    setup_command.setString("curl -X GET \"https://api.box.com/2.0/users/me?fields=enterprise\" -H \"Authorization: Bearer ACCESS_TOKEN\"") catch {};
    setup_replacements = setup_command.replace("ACCESS_TOKEN", try curl.getVar(alloc, "access_token"));
    success = if (curl.addFunction("getEnterprise", try setup_command.getAsString(alloc))) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    // Register output rules once. Chilkat 11.7.0 evaluates {{name}} variables
    // when extracting each output, using the current value at that time.
    success = if (curl.addOutput2("findParent", "entries", "name", "{{parent_name}}", true, "id", "parent_id")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    success = if (curl.addOutput2("findFolder", "entries", "name", "{{folder_name}}", true, "id", "folder_id")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    success = if (curl.addOutput2("findFile", "entries", "name", "{{file_name}}", true, "id", "file_id")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    success = if (curl.addOutput2("findDestination", "entries", "name", "{{destination_name}}", true, "id", "destination_id")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    success = if (curl.addOutput("getFileDetails", "file_version.id", "file_version_id")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    success = if (curl.addOutput("getFileDetails", "sha1", "file_sha1")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    success = if (curl.addOutput("getEnterprise", "enterprise.id", "enterprise_id")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    }

    // END ONE-TIME SETUP
    // The name lookups below assume each listing fits in one page (1000 items).
    // Parent and destination names select root children; folder_name selects a child
    // of parent_name. "/" selects the root parent. These are not arbitrary paths.

    // BEGIN OPERATION -- keep the configured object when reusing it.
    // Inputs are names. Retain cached IDs when the selected names have not changed.
    const parent_name = "/";
    const folder_name = "documents";
    if ((!std.mem.eql(u8, try curl.getVar(alloc, "parent_name"), parent_name)) or ((std.mem.eql(u8, parent_name, "/")) and (!curl.varDefined("parent_id")))) {
        curl.setVar("parent_name", parent_name);
        curl.clearVar("parent_id");
        curl.clearVar("folder_id");
        curl.clearVar("file_id");
        curl.clearVar("file_version_id");
        curl.clearVar("file_sha1");
        if (std.mem.eql(u8, parent_name, "/")) {
            curl.setVar("parent_id", "0");
        }
    }

    if (!std.mem.eql(u8, try curl.getVar(alloc, "folder_name"), folder_name)) {
        curl.setVar("folder_name", folder_name);
        curl.clearVar("folder_id");
        curl.clearVar("file_id");
        curl.clearVar("file_version_id");
        curl.clearVar("file_sha1");
    }

    // Resolve the named destination first. HttpCurl does not expand variables
    // inside multipart -F attributes in the tested build, so this upload is two phases.
    curl.clearTargetOutput("*");
    const authorized_command1 = try chilkat.StringBuilder.init();
    defer authorized_command1.deinit();
    authorized_command1.setString("curl -X GET \"https://api.box.com/2.0/folders/{{folder_id}}?fields=id\" -H \"Authorization: Bearer {{access_token}}\"") catch {};
    _ = authorized_command1.replace("{{access_token}}", try curl.getVar(alloc, "access_token"));
    success = if (curl.doYourThing(try authorized_command1.getAsString(alloc))) true else |_| false;
    if ((!success) or (curl.getStatusCode() != 200)) {
        std.debug.print("Box request failed. HTTP status: {d}\n", .{curl.getStatusCode()});
        std.debug.print("Failure reason: {d}\n", .{curl.getFailReason()});
        std.debug.print("{s}\n", .{try curl.getResponseBodyStr(alloc)});
        return;
    }

    // This example limits size to 2 GB so offsets fit all generated languages' integers.
    // The source must not change during hashing or upload. Create qa_output first.
    const local_path = "qa_data/large-document.zip";
    const file = try chilkat.FileAccess.init();
    defer file.deinit();
    // Check the decimal size before converting so an oversized file cannot overflow int.
    const size_string = file.fileSizeStr(alloc, local_path) catch {
        std.debug.print("{s}\n", .{try file.getLastErrorText(alloc)});
        return;
    };

    const size_text = try chilkat.StringBuilder.init();
    defer size_text.deinit();
    size_text.setString(size_string) catch {};
    const size_match = try chilkat.JsonObject.init();
    defer size_match.deinit();
    if (size_text.regexMatch("^([0-9]{1,9}|1[0-9]{9}|2000000000)$", size_match, 100) != 1) {
        std.debug.print("Choose an input between 20 MB and 2 GB for this example.\n", .{});
        return;
    }

    const file_size = (std.fmt.parseInt(i32, size_string, 10) catch 0);
    if (file_size < 20971520) {
        std.debug.print("Choose an input between 20 MB and 2 GB for this example.\n", .{});
        return;
    }

    const http = try chilkat.Http.init();
    defer http.deinit();
    http.setAuthToken(try curl.getVar(alloc, "access_token"));
    const response = try chilkat.HttpResponse.init();
    defer response.deinit();
    const request = try chilkat.JsonObject.init();
    defer request.deinit();
    request.updateString("folder_id", try curl.getVar(alloc, "folder_id")) catch {};
    request.updateString("file_name", "large-document.zip") catch {};
    request.updateInt("file_size", file_size) catch {};
    success = if (http.httpJson("POST", "https://upload.box.com/api/2.0/files/upload_sessions", request, "application/json", response)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    }

    if (response.getStatusCode() != 201) {
        std.debug.print("{s}\n", .{try response.getBodyStr(alloc)});
        return;
    }

    const session = try chilkat.JsonObject.init();
    defer session.deinit();
    success = if (session.load(try response.getBodyStr(alloc))) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try session.getLastErrorText(alloc)});
        return;
    }

    success = if (session.writeFile("qa_output/box_upload_session.json")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try session.getLastErrorText(alloc)});
        return;
    }

    const part_size = session.intOf("part_size");
    const upload_url = try session.stringOf(alloc, "session_endpoints.upload_part");
    const commit_url = try session.stringOf(alloc, "session_endpoints.commit");
    if ((part_size <= 0) or ((std.mem.eql(u8, upload_url, "")) or (std.mem.eql(u8, commit_url, "")))) {
        std.debug.print("Invalid session response.\n", .{});
        return;
    }

    const hash = try chilkat.Crypt2.init();
    defer hash.deinit();
    hash.setHashAlgorithm("sha1");
    hash.setEncodingMode("base64");
    const file_digest = hash.hashFileENC(alloc, local_path) catch {
        std.debug.print("{s}\n", .{try hash.getLastErrorText(alloc)});
        return;
    };

    success = if (file.fileOpen(local_path, 2147483648, 1, 3, 0)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try file.getLastErrorText(alloc)});
        return;
    }

    const commit = try chilkat.JsonObject.init();
    defer commit.deinit();
    const part_response = try chilkat.JsonObject.init();
    defer part_response.deinit();
    const chunk = try chilkat.BinData.init();
    defer chunk.deinit();
    const range = try chilkat.StringBuilder.init();
    defer range.deinit();
    const digest_header = try chilkat.StringBuilder.init();
    defer digest_header.deinit();
    var offset: i32 = 0;
    var part_index: i32 = 0;
    var part_length: i32 = 0;
    var last_byte: i32 = 0;
    var part_digest: [:0]const u8 = "";
    while (offset < file_size) {
        chunk.clear() catch {};
        success = if (file.fileReadBd(part_size, chunk)) true else |_| false;
        if (!success) {
            std.debug.print("{s}\n", .{try file.getLastErrorText(alloc)});
            file.fileClose();
            return;
        }

        part_length = chunk.getNumBytes();
        if (part_length <= 0) {
            std.debug.print("Unexpected end of file.\n", .{});
            file.fileClose();
            return;
        }

        last_byte = offset + (part_length - 1);
        range.clear();
        range.append("bytes ") catch {};
        range.appendInt(offset) catch {};
        range.append("-") catch {};
        range.appendInt(last_byte) catch {};
        range.append("/") catch {};
        range.appendInt(file_size) catch {};
        part_digest = hash.hashBdENC(alloc, chunk) catch {
            std.debug.print("{s}\n", .{try hash.getLastErrorText(alloc)});
            file.fileClose();
            return;
        };

        digest_header.setString("sha=") catch {};
        digest_header.append(part_digest) catch {};
        http.setRequestHeader("Digest", try digest_header.getAsString(alloc));
        http.setRequestHeader("Content-Range", try range.getAsString(alloc));
        success = if (http.httpBd("PUT", upload_url, chunk, "application/octet-stream", response)) true else |_| false;
        if ((!success) or (response.getStatusCode() != 200)) {
            std.debug.print("Part failed. HTTP status: {d}\n", .{response.getStatusCode()});
            std.debug.print("{s}\n", .{try response.getBodyStr(alloc)});
            std.debug.print("Inspect the saved session and server list_parts before retrying this part.\n", .{});
            file.fileClose();
            return;
        }

        part_response.load(try response.getBodyStr(alloc)) catch {
            file.fileClose();
            return;
        };

        commit.setI(part_index);
        // Copy the server's acknowledged part fields into the commit manifest.
        commit.updateString("parts[i].part_id", try part_response.stringOf(alloc, "part.part_id")) catch {};
        commit.updateInt("parts[i].offset", part_response.intOf("part.offset")) catch {};
        commit.updateInt("parts[i].size", part_response.intOf("part.size")) catch {};
        commit.updateString("parts[i].sha1", try part_response.stringOf(alloc, "part.sha1")) catch {};
        commit.writeFile("qa_output/box_upload_parts.json") catch {
            std.debug.print("{s}\n", .{try commit.getLastErrorText(alloc)});
            file.fileClose();
            return;
        };

        offset = offset + part_length;
        part_index = part_index + 1;
    }

    file.fileClose();
    http.removeRequestHeader("Content-Range");
    digest_header.setString("sha=") catch {};
    digest_header.append(file_digest) catch {};
    http.setRequestHeader("Digest", try digest_header.getAsString(alloc));
    var attempts: i32 = 0;
    var wait_seconds: i32 = 0;
    const retry_header = try chilkat.StringBuilder.init();
    defer retry_header.deinit();
    const retry_match = try chilkat.JsonObject.init();
    defer retry_match.deinit();
    var completed: bool = false;
    while ((attempts < 10) and (!completed)) {
        http.httpJson("POST", commit_url, commit, "application/json", response) catch {
            std.debug.print("Commit failed: {d}\n", .{response.getStatusCode()});
            std.debug.print("{s}\n", .{try response.getBodyStr(alloc)});
            return;
        };

        if (response.getStatusCode() == 201) {
            completed = true;
        } else {
            if (response.getStatusCode() != 202) {
                std.debug.print("Unexpected commit status: {d}\n", .{response.getStatusCode()});
                return;
            }

            // Box's asynchronous commit response supplies Retry-After in seconds.
            retry_header.setString(try response.getHeaderField(alloc, "Retry-After")) catch {};
            if (retry_header.regexMatch("^[0-9]{1,2}$", retry_match, 100) != 1) {
                std.debug.print("Commit pending. Preserve the session and inspect Retry-After before resuming: {s}\n", .{try retry_header.getAsString(alloc)});
                return;
            }

            wait_seconds = (std.fmt.parseInt(i32, try retry_header.getAsString(alloc), 10) catch 0);
            if ((wait_seconds < 1) or (wait_seconds > 60)) {
                std.debug.print("Commit pending. Resume commit after Retry-After: {s}\n", .{try response.getHeaderField(alloc, "Retry-After")});
                return;
            }

            http.sleepMs(wait_seconds * 1000);
        }

        attempts = attempts + 1;
    }

    if (!completed) {
        std.debug.print("Commit still pending; retain session and part manifests to resume commit later.\n", .{});
        return;
    }

    std.debug.print("Upload committed: {s}\n", .{try response.getBodyStr(alloc)});
}