Uploads: binary-safe simple uploads (one PUT) for small files, and resumable upload sessions (createUploadSession) for large ones. upload-file and replace-content pick for you: simple up to 4 MiB, a session above.

Content may be Bytes (sent verbatim), a Str (its UTF-8 bytes), or a Map/Vec (sent as JSON).

Upload sessions. create-upload-session returns an UploadSession with a pre-authenticated upload-url (keep it to resume). Bytes are PUT to it in order, in chunks that are multiples of 320 KiB (327,680 bytes) and under 60 MiB; the default is 10 MiB (opts.chunk-size, rounded down to a 320 KiB multiple; Microsoft recommends 5-10 MiB). Chunk PUTs carry no Authorization header (Graph may answer 401 if they do). A chunk whose connection drops is resent once, at once; any other failure (429, 5xx, 416) stops with an Err: call resume-upload later (for example from a run retry policy), which asks the session for nextExpectedRanges and sends the rest. A session expires when no chunk arrives before its expiration; 404 means start again. cancel-upload-session discards one.

Conflicts. opts.conflict is "replace" (default for upload-file; OneDrive keeps the old content as a version), "rename" or "fail".

Permissions. Files.ReadWrite (own drive), Files.ReadWrite.All, Sites.ReadWrite.All, or Sites.Selected / Files.SelectedOperations.Selected with a write grant. App-only cannot replace the content of a file protected by a sensitivity label.

Values

CHUNK_ALIGN

CHUNK_ALIGN: Int 327680

DEFAULT_CHUNK_SIZE

DEFAULT_CHUNK_SIZE: Int 10485760

DEFAULT_CONFLICT

DEFAULT_CONFLICT: Str "replace"

MAX_CHUNK_SIZE

MAX_CHUNK_SIZE: Int 62586880

SIMPLE_UPLOAD_LIMIT

SIMPLE_UPLOAD_LIMIT: Int 4194304

Functions

align-chunk-size

fn (size: Int): Int

Round a chunk size down to a multiple of 320 KiB (327,680 bytes), at least 320 KiB and under 60 MiB (pure).

Example

::microsoft::onedrive::upload/align-chunk-size(10485760)  // 10485760
::microsoft::onedrive::upload/align-chunk-size(1000000)  // 983040

build-session-body

fn (conflict: Str, opts: Map): Map

The createUploadSession body (pure): {item: {@microsoft.graph.conflictBehavior, description?, fileSize?}, deferCommit?}. Unset options are omitted.

Example

::microsoft::onedrive::upload/build-session-body("rename", {file-size: 1024})  // {item: {"@microsoft.graph.conflictBehavior": "rename", fileSize: 1024}}

cancel-upload-session

fn (upload-url: Str): Bool
fn (upload-url: Str, opts: Map): Bool

DELETE an upload URL (no Authorization) - discard the session and its uploaded bytes. Returns true.

Example

session ::microsoft::onedrive::upload/create-upload-session("/Backups", "db.dump")
::microsoft::onedrive::upload/cancel-upload-session(session.upload-url)

chunk-bounds

fn (offset: Int, chunk-size: Int, total: Int): Vec

The [start, end) of the chunk beginning at offset (pure): end is offset + chunk-size, capped at total.

Example

::microsoft::onedrive::upload/chunk-bounds(0, 10, 25)  // [0, 10]
::microsoft::onedrive::upload/chunk-bounds(20, 10, 25)  // [20, 25]

classify-chunk-response

fn (response: Result, assumed-offset: Int?): Map

Interpret the response to a chunk PUT (pure): {state: "complete", item} (200/201), {state: "incomplete", next-offset} (202, from nextExpectedRanges; assumed-offset when Graph lists none), or {state: "retry", error} for a dropped connection; an Err for an expired session (404), a range mismatch (416: call resume-upload), a name conflict (409) or any other failure (429/5xx carry retry-after).

Example

::microsoft::onedrive::upload/classify-chunk-response({status: 202, headers: {}, body: {nextExpectedRanges: ["26-"]}}, 26)  // {state: "incomplete", next-offset: 26}
::microsoft::onedrive::upload/classify-chunk-response({status: 201, headers: {}, body: {id: "01ABC"}}, null).item.id  // "01ABC"

content-range

fn (start: Int, end: Int, total: Int): Str

The Content-Range for bytes start up to (not including) end of a total-byte upload: content-range(0, 26, 128) is "bytes 0-25/128" (pure).

Example

::microsoft::onedrive::upload/content-range(0, 26, 128)  // "bytes 0-25/128"

create-replace-session

fn (item: Any): UploadSession
fn (item: Any, opts: Map): UploadSession

POST {item}/createUploadSession - open a session that replaces an existing file's content. opts as for create-upload-session, plus if-match (eTag).

Example

item ::microsoft::onedrive::items/get-item("/Backups/db.dump")
session ::microsoft::onedrive::upload/create-replace-session(item, {if-match: item.eTag})
session.upload-url

create-upload-session

fn (parent: Any, name: Str): UploadSession
fn (parent: Any, name: Str, opts: Map): UploadSession

POST {parent}:/{name}:/createUploadSession - open a session for a new file name under parent (an id, "root", a /path, or a Map). opts.conflict (replace default, rename, fail), description, file-size (personal OneDrive: checks quota up front), defer-commit, drive. Returns an UploadSession.

Example

session ::microsoft::onedrive::upload/create-upload-session("/Backups", "db.dump", {conflict: "rename"})

// Keep this to resume
session.upload-url

next-offset-from-ranges

fn (ranges: Any): Int?

The next byte to send from an upload session's nextExpectedRanges (pure): the start of the first range (["26-"] is 26, ["12345-55232", "77829-99375"] is 12345); null when the list is empty or missing.

Example

::microsoft::onedrive::upload/next-offset-from-ranges(["26-"])  // 26
::microsoft::onedrive::upload/next-offset-from-ranges(["12345-55232", "77829-99375"])  // 12345
::microsoft::onedrive::upload/next-offset-from-ranges([])  // null

put-chunk

fn (upload-url: Str, chunk: Bytes, start: Int, total: Int): Any

PUT one chunk to an upload URL, without an Authorization header, with its Content-Range. Returns the raw response or a transport Err.

Example

session ::microsoft::onedrive::upload/create-upload-session("/Backups", "note.txt")
chunk Bytes("hello")
response ::microsoft::onedrive::upload/put-chunk(session.upload-url, chunk, 0, length(chunk))
response.status

replace-content

fn (item: Any, content: Any, mime-type: Str?): Map
fn (item: Any, content: Any, mime-type: Str?, opts: Map): Map

Replace an existing file's content (a new version): a simple PUT to {item}/content up to 4 MiB, an upload session above. opts.if-match (eTag) guards against overwriting someone else's change. Returns the driveItem.

Example

item ::microsoft::onedrive::items/get-item("/Reports/q3.csv")

// Fails with 412 if someone changed the file first
::microsoft::onedrive::upload/replace-content(item, "region,total\nwest,42\n", "text/csv", {if-match: item.eTag})

resume-upload

fn (upload-url: Str, content: Any): Map
fn (upload-url: Str, content: Any, opts: Map): Map

Continue an interrupted upload of content to upload-url: ask the session where it stands, then send the rest. Returns the driveItem.

saved-upload-url "https://contoso-my.sharepoint.com/_api/v2.0/drives/b!xyz/items/01ABC/uploadSession?guid=abc"
bytes ::microsoft::onedrive::content/download("/Raw/video.mp4").bytes
item ::microsoft::onedrive::upload/resume-upload(saved-upload-url, bytes)

to-bytes

fn (content: Any): Bytes

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

Example

length(::microsoft::onedrive::upload/to-bytes("hi"))  // 2
length(::microsoft::onedrive::upload/to-bytes(null))  // 0
Str(::microsoft::onedrive::upload/to-bytes([1, 2]))  // "[1,2]"

upload-file

fn (parent: Any, name: Str, content: Any, mime-type: Str?): Map
fn (parent: Any, name: Str, content: Any, mime-type: Str?, opts: Map): Map

Upload content as name under parent (an id, "root", a /path or a Map): a simple PUT up to 4 MiB, an upload session above (or with opts.resumable: true). opts.conflict (replace default, rename, fail), chunk-size, drive. Returns the driveItem.

item ::microsoft::onedrive::upload/upload-file("/Reports", "q3.csv", "region,total\nwest,42\n", "text/csv")
video-bytes ::microsoft::onedrive::content/download("/Raw/video.mp4").bytes
big ::microsoft::onedrive::upload/upload-file("root", "video.mp4", video-bytes, "video/mp4", {conflict: "rename"})

upload-large

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

Upload content as name under parent through an upload session (any size). Returns the driveItem. See create-upload-session and upload-to-session for opts.

Example

item ::microsoft::onedrive::upload/upload-large("/Backups", "db.dump", Bytes("backup contents"), {conflict: "rename", chunk-size: 5242880})
item.id

upload-session-status

fn (upload-url: Str): Map
fn (upload-url: Str, opts: Map): Map

GET an upload URL (no Authorization) - how far the session got: {next-offset, next-expected-ranges, expiration}. An Err when the session is gone (404).

Example

url "https://contoso-my.sharepoint.com/_api/v2.0/drives/b!xyz/items/01ABC/uploadSession?guid=abc"
status ::microsoft::onedrive::upload/upload-session-status(url)
status.next-offset

upload-small

fn (parent: Any, name: Str, content: Any, mime-type: Str?): Map
fn (parent: Any, name: Str, content: Any, mime-type: Str?, opts: Map): Map

PUT {parent}:/{name}:/content - create or replace a file in one request (best up to 4 MiB; Graph accepts up to 250 MB, but a session survives dropped connections). opts.conflict (replace default, rename, fail), drive. Returns the driveItem.

Example

item ::microsoft::onedrive::upload/upload-small("/Reports", "notes.txt", "First draft\n", "text/plain", {conflict: "fail"})
item.id

upload-to-session

fn (upload-url: Str, content: Any): Map
fn (upload-url: Str, content: Any, opts: Map): Map

Send content to an open session's upload-url, starting at byte 0 (or opts.offset), in opts.chunk-size chunks (default 10 MiB, rounded down to a 320 KiB multiple). Returns the created driveItem.

Example

session ::microsoft::onedrive::upload/create-upload-session("/Backups", "db.dump")
item ::microsoft::onedrive::upload/upload-to-session(session.upload-url, Bytes("backup contents"), {chunk-size: 5242880})
item.id

Types

UploadSession

UploadSession type {
    upload-url: Str,
    expiration: Str?,
    next-expected-ranges: Vec
}

An open upload session.

  • upload-url - the pre-authenticated URL for chunk PUTs; treat it as a secret, and keep it to resume
  • expiration - when the session lapses without activity
  • next-expected-ranges - Graph's nextExpectedRanges