Uploads: binary-safe multipart uploads, new versions of existing files, and chunked upload sessions for large files. upload-file and upload-version switch to a chunked session at 20 MB (Box's minimum for sessions; simple uploads are limited to 50 MB).

Content may be Bytes (sent verbatim), a Str (its UTF-8 bytes), or a Map/Vec (sent as JSON). Bodies are assembled as Bytes, so binary data (0x00, 0xFF, invalid UTF-8) arrives intact. Every upload sends the content's SHA-1 so Box rejects a corrupted transfer.

Chunked sessions. create-upload-session returns Box's part_size; each part (exactly part_size bytes, except the last) is PUT with Digest: sha=<base64 SHA-1> and Content-Range, then commit-upload-session sends every part plus the whole file's SHA-1. Box can answer the commit with 202 while it assembles the file: upload-chunked then returns state: "processing", and finish-upload commits again later (a schedule or a later step; nothing here waits). A failed part aborts the session unless opts.keep-session is true. Sessions expire after 7 days. The whole file is held in memory.

Preflight. preflight-check (OPTIONS /files/content) checks name conflicts, quota and size limits without sending content: the same 409 item_name_in_use or 403 storage_limit_exceeded the upload itself would hit.

Values

CHUNKED_THRESHOLD

CHUNKED_THRESHOLD: Int 20000000

LOCAL_KEYS

LOCAL_KEYS: Vec [
  "parent-id",
  "content-created-at",
  "content-modified-at",
  "description",
  "attributes",
  "chunked",
  "keep-session",
  "if-match",
  "if-none-match",
  "boxapi",
  "name"
]

MAX_PARTS_PAGE

MAX_PARTS_PAGE: Int 1000

SIMPLE_UPLOAD_LIMIT

SIMPLE_UPLOAD_LIMIT: Int 50000000

Functions

abort-upload-session

fn (session-id: Str): Bool
fn (session-id: Str, opts: Map): Bool

DELETE /files/upload_sessions/{id} - abort a session and discard its parts. Returns true.

Example

::box::upload/abort-upload-session("F971964745A5CD0C001BBE4E58196BFD")

build-multipart

fn (attributes: Map, content: Any, filename: Str, boundary: Str): Bytes

Build the multipart/form-data upload body as Bytes (pure): the attributes JSON part first (Box requires it before the file), then the file part. Content is concatenated as bytes, never interpolated into a string, so binary payloads survive.

Example

body ::box::upload/build-multipart({name: "a.txt"}, "hi", "a.txt", "b1")
contains(Str(body), "name=\"attributes\"")  // true
ends-with(Str(body), "--b1--\r\n")  // true

commit-body

fn (parts: Vec, attributes: Map?): Map

The commit body (pure): parts sorted by offset, plus optional attributes (e.g. content_modified_at, description).

Example

::box::upload/commit-body([{part_id: "a", offset: 0, size: 5}], null)  // {parts: [{part_id: "a", offset: 0, size: 5}]}

commit-upload-session

fn (session-id: Str, parts: Vec, digest: Str): ChunkedUpload
fn (session-id: Str, parts: Vec, digest: Str, opts: Map): ChunkedUpload

POST /files/upload_sessions/{id}/commit - assemble the file from parts (as returned by upload-part / list-all-parts). digest is digest-header(whole-file-bytes). Returns a ChunkedUpload (state: "processing" on Box's 202). Options: attributes, if-match (for versions), credentials.

Example

content Bytes("hello")
session-id "F971964745A5CD0C001BBE4E58196BFD"
parts ::box::upload/list-all-parts(session-id)
done ::box::upload/commit-upload-session(session-id, parts, ::box::upload/digest-header(content))
if(eq(done.state, "committed"), done.file.id, done.retry-after)

create-session-body

fn (folder-id: Str, name: Str, size: Int): Map

The POST /files/upload_sessions body (pure): folder_id, file_size, file_name.

Example

::box::upload/create-session-body("0", "a.bin", 100)  // {folder_id: "0", file_size: 100, file_name: "a.bin"}

create-upload-session

fn (folder-id: Str, name: Str, size: Int): Map
fn (folder-id: Str, name: Str, size: Int, opts: Map): Map

POST /files/upload_sessions - open a chunked upload of a new file (size bytes, at least 20 MB). Returns Box's session: id, part_size, total_parts, num_parts_processed, session_expires_at, session_endpoints.

Example

session ::box::upload/create-upload-session("12345", "video.mp4", 52428800)
session.id
session.part_size

create-version-session

fn (file-id: Str, size: Int): Map
fn (file-id: Str, size: Int, opts: Map): Map

POST /files/{id}/upload_sessions - open a chunked upload of a new version (opts.name renames the file).

Example

session ::box::upload/create-version-session("12345", 52428800, {name: "video-v2.mp4"})
session.part_size

digest-header

fn (content: Any): Str

The Digest header value for a part or a whole file (pure): sha=<base64 SHA-1>.

Example

::box::upload/digest-header("hello")  // "sha=qvTGHdzF6KLavt4PO0gs2a6pQ00="

finish-upload

fn (session-id: Str, content: Any): ChunkedUpload
fn (session-id: Str, content: Any, opts: Map): ChunkedUpload

Commit a session again after Box answered processing: lists its parts, recomputes the whole-file digest from content, and commits. Returns a ChunkedUpload.

Example

content Bytes("hello")
done ::box::upload/finish-upload("F971964745A5CD0C001BBE4E58196BFD", content)
done.state

first-entry

fn (response: Any): Any

The file in an upload response ({total_count, entries: [file]}), or the response itself (pure).

Example

::box::upload/first-entry({total_count: 1, entries: [{id: "1"}]})  // {id: "1"}
::box::upload/first-entry({id: "2"})  // {id: "2"}

get-upload-session

fn (session-id: Str): Map
fn (session-id: Str, opts: Map): Map

GET /files/upload_sessions/{id} - a session's state (num_parts_processed, total_parts, expiry).

Example

session ::box::upload/get-upload-session("F971964745A5CD0C001BBE4E58196BFD")
session.num_parts_processed

list-all-parts

fn (session-id: Str): Vec
fn (session-id: Str, opts: Map): Vec

Every part of a session (all offset pages), sorted by offset.

Example

parts ::box::upload/list-all-parts("F971964745A5CD0C001BBE4E58196BFD")
length(parts)

list-parts

fn (session-id: Str): Map
fn (session-id: Str, opts: Map): Map

GET /files/upload_sessions/{id}/parts - one page of the parts Box has ({entries, total_count, offset, limit}; offset paging).

Example

page ::box::upload/list-parts("F971964745A5CD0C001BBE4E58196BFD", {limit: 100})
page.total_count
page.entries

new-boundary

fn (): Str

A random multipart boundary, so file content cannot collide with it.

Example

starts-with(::box::upload/new-boundary(), "hot-box-")  // true

part-headers

fn (part: Bytes, offset: Int, total: Int): Map

The headers for uploading one part (pure): Digest, Content-Range, Content-Type: application/octet-stream.

Example

headers ::box::upload/part-headers(Bytes("hello"), 0, 5)
headers.Content-Range  // "bytes 0-4/5"
headers.Digest  // "sha=qvTGHdzF6KLavt4PO0gs2a6pQ00="

part-offsets

fn (total: Int, part-size: Int): Vec

The [offset, size] of every part of a total-byte file in part-size parts (pure): all part-size except a shorter last one.

Example

::box::upload/part-offsets(10, 4)  // [[0, 4], [4, 4], [8, 2]]

part-range

fn (offset: Int, size: Int, total: Int): Str

The Content-Range for a part (pure): bytes offset through offset + size - 1 of total, e.g. "bytes 0-8388607/52428800".

Example

::box::upload/part-range(0, 8388608, 52428800)  // "bytes 0-8388607/52428800"

preflight-body

fn (name: Str?, parent-id: Str?, size: Int?): Map

The preflight body (pure): name, parent: {id} (null for a new version) and size in bytes.

Example

::box::upload/preflight-body("a.txt", "0", 10)  // {name: "a.txt", parent: {id: "0"}, size: 10}

preflight-check

fn (name: Str, parent-id: Str, size: Int): Map
fn (name: Str, parent-id: Str, size: Int, opts: Map): Map

OPTIONS /files/content - ask Box whether name (size bytes) can be uploaded into parent-id: returns {upload_url, upload_token} when it can, or an HttpError (409 item_name_in_use with the conflict in context-info, 403 storage_limit_exceeded, file_size_limit_exceeded).

Example

check ::box::upload/preflight-check("report.csv", "12345", 2048)
if(is-err(check), err-message(check), check.upload_url)

preflight-version

fn (file-id: Str, size: Int): Map
fn (file-id: Str, size: Int, opts: Map): Map

OPTIONS /files/{id}/content - the preflight check for a new version (optionally renamed). Answers as preflight-check does.

Example

check ::box::upload/preflight-version("12345", 2048, {name: "report-v2.csv"})
if(is-err(check), err-message(check), check.upload_url)

safe-filename

fn (name: Str): Str

A filename for the multipart header (pure): quotes, backslashes, CR and LF replaced with _. Box names the file from attributes.name, so this only labels the part.

Example

::box::upload/safe-filename("a\"b.txt")  // "a_b.txt"
::box::upload/safe-filename("")  // "file"

sha1-base64

fn (content: Any): Str

The SHA-1 of content as base64 of the raw digest, the form Digest: sha= takes.

Example

::box::upload/sha1-base64("hello")  // "qvTGHdzF6KLavt4PO0gs2a6pQ00="

sha1-hex

fn (content: Any): Str

The SHA-1 of content as lowercase hex (Box's sha1 field and the Content-MD5 upload header, which Box uses for SHA-1).

Example

::box::upload/sha1-hex("hello")  // "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"

sort-parts

fn (parts: Vec): Vec

Parts sorted by offset, keeping only part_id, offset, size, sha1 (the commit body's shape; pure).

Example

::box::upload/sort-parts([{part_id: "b", offset: 4, extra: 1}, {part_id: "a", offset: 0}])  // [{part_id: "a", offset: 0}, {part_id: "b", offset: 4}]

to-bytes

fn (content: Any): Bytes

Upload content as Bytes: Bytes pass through, a Str becomes its UTF-8 bytes, null is empty, and anything else is JSON-encoded.

Example

length(::box::upload/to-bytes("hello"))  // 5
length(::box::upload/to-bytes(null))  // 0

upload-attributes

fn (name: Str?, parent-id: Str?, opts: Map): Map

The attributes part of an upload (pure): name, parent: {id} (null for a new version), content_created_at / content_modified_at (RFC 3339) from opts.content-created-at / content-modified-at, description, then any extra fields in opts.attributes. Nulls are dropped.

Example

::box::upload/upload-attributes("a.txt", "0", {description: "Notes"})  // {name: "a.txt", parent: {id: "0"}, description: "Notes"}

// A new version has no parent
::box::upload/upload-attributes(null, null, {})  // {}

upload-chunked

fn (folder-id: Str, name: Str, content: Any): ChunkedUpload
fn (folder-id: Str, name: Str, content: Any, opts: Map): ChunkedUpload

Upload a large file (20 MB or more) through a chunked session: create it, send every part, commit. Returns a ChunkedUpload; state: "processing" means Box is still assembling it, so call finish-upload later.

big-bytes ::box::content/download-file("67890").bytes
done ::box::upload/upload-chunked("12345", "video.mp4", big-bytes, {})
if(eq(done.state, "committed"), done.file.id, done.session-id)

upload-file

fn (name: Str, content: Any): Map
fn (name: Str, content: Any, parent-id: Str): Map
fn (name: Str, content: Any, parent-id: Str, opts: Map): Map

Upload a new file into parent-id (default the root, "0"). Returns the file. Options: content-created-at / content-modified-at (RFC 3339), description, attributes (extra fields), chunked (true / false to force; default chunked from 20 MB), fields, plus credentials. A name already used in the folder fails with 409 item_name_in_use; ::box::folders/conflict-item reads the existing file from that error, and upload-version replaces its content.

csv-text "name,total\nAna,12\n"
file ::box::upload/upload-file("report.csv", csv-text, "12345")
file.id

upload-part

fn (session-id: Str, part: Bytes, offset: Int, total: Int): Map
fn (session-id: Str, part: Bytes, offset: Int, total: Int, opts: Map): Map

PUT /files/upload_sessions/{id} - upload one part: part bytes starting at offset of a total-byte file. Returns Box's part record {part_id, offset, size, sha1}, which commit-upload-session needs. Re-sending the same part is safe.

Example

content Bytes("hello")
part ::box::upload/upload-part("F971964745A5CD0C001BBE4E58196BFD", content, 0, length(content))
part.part_id

upload-to-session

fn (session: Map, content: Any, opts: Map): ChunkedUpload

Upload content into an open session (from create-upload-session / create-version-session) part by part, then commit. Returns a ChunkedUpload. A failed part aborts the session unless opts.keep-session is true.

Example

content Bytes("hello")
session ::box::upload/create-upload-session("12345", "a.bin", length(content))
done ::box::upload/upload-to-session(session, content, {keep-session: true})
done.state

upload-version

fn (file-id: Str, content: Any): Map
fn (file-id: Str, content: Any, opts: Map): Map

POST /files/{id}/content - upload new content for an existing file (a new version). opts.name renames it at the same time; opts.if-match (the file's etag) fails with 412 when someone changed it first. Chunked from 20 MB. Returns the file.

Example

file ::box::upload/upload-version("12345", "col1,col2\n1,2\n")
file.file_version.id

// Fails with 412 if someone changed it first
safe ::box::upload/upload-version("12345", "col1,col2\n3,4\n", {if-match: file.etag})
safe.etag

Types

ChunkedUpload

ChunkedUpload type {
    state: Str,
    file: Map?,
    session-id: Str,
    parts: Vec,
    digest: Str,
    retry-after: Int?
}

The outcome of a chunked upload.

  • state - "committed" (the file exists) or "processing" (Box answered the commit with 202; commit again later with finish-upload)
  • file - the file when committed, else null
  • session-id, parts - the session and its parts
  • digest - the whole-file Digest header value
  • retry-after - seconds Box asked to wait (when processing)