Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

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

local 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.
local curl = chilkat.HttpCurl.new()
curl:set_var("access_token", "BOX_ACCESS_TOKEN")
curl:set_var("parent_name", "/")
-- Box's fixed root ID is an implementation detail; callers supply names.
curl:set_var("parent_id", "0")
local setupCommand = chilkat.StringBuilder.new()

-- Register findParent; registration sends no request.
setupCommand:set_string("curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
local setupReplacements = setupCommand:replace("ACCESS_TOKEN", curl:get_var("access_token"))
success = curl:add_function("findParent", setupCommand:get_as_string())
if not success then
    print(curl.last_error_text)
    return
end

-- Register findFolder; registration sends no request.
setupCommand:set_string("curl -X GET \"https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand:replace("ACCESS_TOKEN", curl:get_var("access_token"))
success = curl:add_function("findFolder", setupCommand:get_as_string())
if not success then
    print(curl.last_error_text)
    return
end

-- Register findFile; registration sends no request.
setupCommand:set_string("curl -X GET \"https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand:replace("ACCESS_TOKEN", curl:get_var("access_token"))
success = curl:add_function("findFile", setupCommand:get_as_string())
if not success then
    print(curl.last_error_text)
    return
end

-- Register findDestination; registration sends no request.
setupCommand:set_string("curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand:replace("ACCESS_TOKEN", curl:get_var("access_token"))
success = curl:add_function("findDestination", setupCommand:get_as_string())
if not success then
    print(curl.last_error_text)
    return
end

-- Register getFileDetails; registration sends no request.
setupCommand:set_string("curl -X GET \"https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand:replace("ACCESS_TOKEN", curl:get_var("access_token"))
success = curl:add_function("getFileDetails", setupCommand:get_as_string())
if not success then
    print(curl.last_error_text)
    return
end

-- Register getEnterprise; registration sends no request.
setupCommand:set_string("curl -X GET \"https://api.box.com/2.0/users/me?fields=enterprise\" -H \"Authorization: Bearer ACCESS_TOKEN\"")
setupReplacements = setupCommand:replace("ACCESS_TOKEN", curl:get_var("access_token"))
success = curl:add_function("getEnterprise", setupCommand:get_as_string())
if not success then
    print(curl.last_error_text)
    return
end

-- Register output rules once. Chilkat 11.7.0 evaluates {{name}} variables
-- when extracting each output, using the current value at that time.
success = curl:add_output2("findParent", "entries", "name", "{{parent_name}}", true, "id", "parent_id")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output2("findFolder", "entries", "name", "{{folder_name}}", true, "id", "folder_id")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output2("findFile", "entries", "name", "{{file_name}}", true, "id", "file_id")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output2("findDestination", "entries", "name", "{{destination_name}}", true, "id", "destination_id")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output("getFileDetails", "file_version.id", "file_version_id")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output("getFileDetails", "sha1", "file_sha1")
if not success then
    print(curl.last_error_text)
    return
end

success = curl:add_output("getEnterprise", "enterprise.id", "enterprise_id")
if not success then
    print(curl.last_error_text)
    return
end

-- 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.
local parentName = "/"
local folderName = "documents"
if (curl:get_var("parent_name") ~= parentName) or ((parentName == "/") and (not curl:var_defined("parent_id"))) then
    curl:set_var("parent_name", parentName)
    curl:clear_var("parent_id")
    curl:clear_var("folder_id")
    curl:clear_var("file_id")
    curl:clear_var("file_version_id")
    curl:clear_var("file_sha1")
    if parentName == "/" then
        curl:set_var("parent_id", "0")
    end
end

if curl:get_var("folder_name") ~= folderName then
    curl:set_var("folder_name", folderName)
    curl:clear_var("folder_id")
    curl:clear_var("file_id")
    curl:clear_var("file_version_id")
    curl:clear_var("file_sha1")
end

-- 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:clear_target_output("*")
local authorizedCommand1 = chilkat.StringBuilder.new()
authorizedCommand1:set_string("curl -X GET \"https://api.box.com/2.0/folders/{{folder_id}}?fields=id\" -H \"Authorization: Bearer {{access_token}}\"")
local headerReplacement1 = authorizedCommand1:replace("{{access_token}}", curl:get_var("access_token"))
success = curl:do_your_thing(authorizedCommand1:get_as_string())
if (not success) or (curl.status_code ~= 200) then
    print("Box request failed. HTTP status: " .. curl.status_code)
    print("Failure reason: " .. curl.fail_reason)
    print(curl.response_body_str)
    return
end

-- 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.
local localPath = "qa_data/large-document.zip"
local file = chilkat.FileAccess.new()
-- Check the decimal size before converting so an oversized file cannot overflow int.
local sizeString = file:file_size_str(localPath)
if not file.last_method_success then
    print(file.last_error_text)
    return
end

local sizeText = chilkat.StringBuilder.new()
sizeText:set_string(sizeString)
local sizeMatch = chilkat.JsonObject.new()
if sizeText:regex_match("^([0-9]{1,9}|1[0-9]{9}|2000000000)$", sizeMatch, 100) ~= 1 then
    print("Choose an input between 20 MB and 2 GB for this example.")
    return
end

local fileSize = tonumber(sizeString)
if fileSize < 20971520 then
    print("Choose an input between 20 MB and 2 GB for this example.")
    return
end

local http = chilkat.Http.new()
http.auth_token = curl:get_var("access_token")
local response = chilkat.HttpResponse.new()
local request = chilkat.JsonObject.new()
request:update_string("folder_id", curl:get_var("folder_id"))
request:update_string("file_name", "large-document.zip")
request:update_int("file_size", fileSize)
success = http:http_json("POST", "https://upload.box.com/api/2.0/files/upload_sessions", request, "application/json", response)
if not success then
    print(http.last_error_text)
    return
end

if response.status_code ~= 201 then
    print(response.body_str)
    return
end

local session = chilkat.JsonObject.new()
success = session:load(response.body_str)
if not success then
    print(session.last_error_text)
    return
end

success = session:write_file("qa_output/box_upload_session.json")
if not success then
    print(session.last_error_text)
    return
end

local partSize = session:int_of("part_size")
local uploadUrl = session:string_of("session_endpoints.upload_part")
local commitUrl = session:string_of("session_endpoints.commit")
if (partSize <= 0) or ((uploadUrl == "") or (commitUrl == "")) then
    print("Invalid session response.")
    return
end

local hash = chilkat.Crypt2.new()
hash.hash_algorithm = "sha1"
hash.encoding_mode = "base64"
local fileDigest = hash:hash_file_enc(localPath)
if not hash.last_method_success then
    print(hash.last_error_text)
    return
end

success = file:file_open(localPath, 2147483648, 1, 3, 0)
if not success then
    print(file.last_error_text)
    return
end

local commit = chilkat.JsonObject.new()
local partResponse = chilkat.JsonObject.new()
local chunk = chilkat.BinData.new()
local range = chilkat.StringBuilder.new()
local digestHeader = chilkat.StringBuilder.new()
local offset = 0
local partIndex = 0
local partLength = 0
local lastByte = 0
local partDigest = ""
while offset < fileSize do
    chunk:clear()
    success = file:file_read_bd(partSize, chunk)
    if not success then
        print(file.last_error_text)
        file:file_close()
        return
    end

    partLength = chunk.num_bytes
    if partLength <= 0 then
        print("Unexpected end of file.")
        file:file_close()
        return
    end

    lastByte = offset + (partLength - 1)
    range:clear()
    range:append("bytes ")
    range:append_int(offset)
    range:append("-")
    range:append_int(lastByte)
    range:append("/")
    range:append_int(fileSize)
    partDigest = hash:hash_bd_enc(chunk)
    if not hash.last_method_success then
        print(hash.last_error_text)
        file:file_close()
        return
    end

    digestHeader:set_string("sha=")
    digestHeader:append(partDigest)
    http:set_request_header("Digest", digestHeader:get_as_string())
    http:set_request_header("Content-Range", range:get_as_string())
    success = http:http_bd("PUT", uploadUrl, chunk, "application/octet-stream", response)
    if (not success) or (response.status_code ~= 200) then
        print("Part failed. HTTP status: " .. response.status_code)
        print(response.body_str)
        print("Inspect the saved session and server list_parts before retrying this part.")
        file:file_close()
        return
    end

    success = partResponse:load(response.body_str)
    if not success then
        file:file_close()
        return
    end

    commit.i = partIndex
    -- Copy the server's acknowledged part fields into the commit manifest.
    commit:update_string("parts[i].part_id", partResponse:string_of("part.part_id"))
    commit:update_int("parts[i].offset", partResponse:int_of("part.offset"))
    commit:update_int("parts[i].size", partResponse:int_of("part.size"))
    commit:update_string("parts[i].sha1", partResponse:string_of("part.sha1"))
    success = commit:write_file("qa_output/box_upload_parts.json")
    if not success then
        print(commit.last_error_text)
        file:file_close()
        return
    end

    offset = offset + partLength
    partIndex = partIndex + 1
end

file:file_close()
http:remove_request_header("Content-Range")
digestHeader:set_string("sha=")
digestHeader:append(fileDigest)
http:set_request_header("Digest", digestHeader:get_as_string())
local attempts = 0
local waitSeconds = 0
local retryHeader = chilkat.StringBuilder.new()
local retryMatch = chilkat.JsonObject.new()
local completed = false
while (attempts < 10) and (not completed) do
    success = http:http_json("POST", commitUrl, commit, "application/json", response)
    if not success then
        print("Commit failed: " .. response.status_code)
        print(response.body_str)
        return
    end

    if response.status_code == 201 then
        completed = true
    else
        if response.status_code ~= 202 then
            print("Unexpected commit status: " .. response.status_code)
            return
        end

        -- Box's asynchronous commit response supplies Retry-After in seconds.
        retryHeader:set_string(response:get_header_field("Retry-After"))
        if retryHeader:regex_match("^[0-9]{1,2}$", retryMatch, 100) ~= 1 then
            print("Commit pending. Preserve the session and inspect Retry-After before resuming: " .. retryHeader:get_as_string())
            return
        end

        waitSeconds = tonumber(retryHeader:get_as_string())
        if (waitSeconds < 1) or (waitSeconds > 60) then
            print("Commit pending. Resume commit after Retry-After: " .. response:get_header_field("Retry-After"))
            return
        end

        http:sleep_ms(waitSeconds * 1000)
    end

    attempts = attempts + 1
end

if not completed then
    print("Commit still pending; retain session and part manifests to resume commit later.")
    return
end

print("Upload committed: " .. response.body_str)