Multipart uploads

Send a file in sequential parts so a failed request only costs one part: the upload resumes from the part that failed instead of starting again. Any file can be sent in parts, down to a single final part, up to its type's limit. Every SDK splits files larger than 8 MiB into parts for you, retries each part and resumes a failed upload; when you produce a file in chunks, send the parts yourself.

Limits

TypeIn one requestIn parts
Images10 MiB20 MiB
Audio and video Preview50 MiB; 120 seconds before billing is set up, 30 minutes after50 MiB and 120 seconds before billing is set up; 2 GiB and 30 minutes after
Documents, text and other files5,000 MiB5,000 MiB

Every part except the final one must be at least 5 MiB, and an upload has at most 10,000 parts. Images larger than 10 MiB must be sent in parts (a single request returns 413 upload/too-large, as does an image in parts over 20 MiB); CalmLens may re-encode an image over 10 MiB to a more efficient format when the upload finishes. With billing set up, audio and video over 50 MiB must be sent in parts, and media over 120 seconds is analysed in sections. Before billing is set up, text and document uploads have the smaller free upload limit described in Usage limits.

Uploading with an SDK

In every SDK, client.assets.create() and client.assets.classify() send content larger than the client's part size as a multipart upload when the SDK knows its size and can read it again, such as bytes, base64 or a file. Streams that can only be read once, and text, are sent in one request. The part size is 8 MiB unless you set it, from 5 MiB to 64 MiB:

SDKPart size option
JavaScriptpartSizeBytes
Pythonpart_size_bytes
GoOptions.PartSizeBytes
Rust.part_size_bytes() on the client builder
Java.partSizeBytes() on the client builder
PHPpartSizeBytes
Rubypart_size_bytes:

The SDK:

  • Sends each part with its own Idempotency-Key, derived from the upload's key (<key>:part-<n>), and retries a part that fails with a network error, a timeout or a temporary server error.
  • Continues with the part CalmLens asks for when a part already arrived, and reads the asset back when the final part already completed the upload.
  • Returns the same asset a single request would.

To resume an upload after the call failed, for example after a restart, call it again with the same idempotencyKey (and, for browser and app uploads, the same upload token). Parts that already arrived are answered from their saved responses within 24 hours, and the upload continues from the first part CalmLens doesn't have. An idempotencyKey for an upload split into parts can be up to 240 characters.

Sending the parts

A part can be sent in either of two forms:

FormFirst partLater partsOptions
Raw bytes, as the SDKs send themPOST /projects/:projectId/assetsPOST /assets/:assetId/partQuery string, with the X-CalmLens-Upload: part header
JSON, as in the requests belowPOST /projects/:projectId/assetsPOST /assets/:assetId/partRequest body, except wait, which is a query parameter
const uploading = await client.assets.create({
  name: "large-image.png",
  part: {
    uint8Array: firstPartBytes,
    type: "image/png",
    partNumber: 1,
    isFinal: false,
  },
});

const completed = await client.assets.uploadPart({
  assetId: uploading.id,
  part: {
    uint8Array: finalPartBytes,
    partNumber: 2,
    isFinal: true,
  },
});

Every upload follows the same rules:

  • Send parts in order, one at a time, with partNumber values starting at 1.
  • Every part except the final one must be at least 5 MiB. A small file can be sent as one final part (partNumber: 1, isFinal: true).
  • The first part sets the file's type (part.type, or the Content-Type of a raw part). It must be a concrete type: application/octet-stream and multipart/* are rejected because CalmLens cannot tell how to process them.
  • sizeInBytes is the size of one part. Raw ReadableStream parts must include it; the SDKs calculate it for buffers, blobs, base64 and byte arrays.
  • In JSON, send the bytes as a base64 string or a uint8Array number array. Split the original bytes first and then base64-encode each part; never split one base64 string into substrings.

1. Start the upload

Send the first part with partNumber: 1, and the size of the whole file as totalSizeInBytes if you know it (see Declaring the file size):

const response = await client.assets.create({
  "name": "large-image.png",
  "part": {
    "base64": "your-part-1-base64",
    "type": "image/png",
    "partNumber": 1,
    "isFinal": false
  },
  "totalSizeInBytes": 15728640
});

The response returns an asset whose upload.status is uploading, with upload.partsReceived counting the parts received. Send the remaining parts to its id.

2. Send more parts

const response = await client.assets.uploadPart({
  "part": {
    "base64": "your-part-2-base64",
    "partNumber": 2,
    "isFinal": false
  },
  "assetId": "asset_5Gm2Rk8XqT3wLp9VbN4c"
});

3. Finish the upload

Mark the last part with isFinal: true:

const response = await client.assets.uploadPart({
  "part": {
    "base64": "your-final-part-base64",
    "partNumber": 3,
    "isFinal": true
  },
  "assetId": "asset_5Gm2Rk8XqT3wLp9VbN4c"
});

The final part completes the upload and returns the asset. A skipped or repeated part number returns 409 upload-part/invalid-number, and a part for an upload that is no longer open returns 409 upload/not-open; see Retrying parts.

Waiting for the decision

Choose wait when you start the upload. CalmLens saves the choice with the upload and applies it when the final part arrives, so the final part can come from another process or a restarted client that only knows the asset ID. Leave wait off later parts to use the saved choice, or pass it on the final part to override it for that upload.

Uploads started without wait are classified synchronously when they finish, as with wait=true. Audio, video and files over 20 MiB are always processed asynchronously, whatever wait says.

Declaring the file size

Pass the size of the whole file as totalSizeInBytes on the first request when you know it. CalmLens then:

  • Rejects an upload that could never finish before storing any of it, for example an image declared above 20 MiB or a video declared above the media limit (413 upload/too-large, with the limit in details.limitBytes).
  • Refuses a part that would take the upload past the declared size, and a final part that leaves it short, with 400 upload/size-mismatch. The refused part is not stored, so you can send the correct part with the same part number.

Without totalSizeInBytes, CalmLens checks the accumulated size as parts arrive and the final total when the upload completes.

Retrying parts

Parts are sequential, so a part sent twice without its Idempotency-Key is refused with 409 upload-part/invalid-number, and its details.expectedPartNumber tells you which part CalmLens needs next. If a request times out, retry it with the same part number: a part that already arrived is refused and you can move on to details.expectedPartNumber, and a part that didn't arrive is accepted. The asset's upload.partsReceived also tells you how many parts arrived.

The final part closes the upload, so a retried final part normally returns 409 upload/not-open even though the upload succeeded. Send an Idempotency-Key header with the first request and with each part to get the original response back instead. See Retrying requests safely.

Last updated

Signing in

Signing out

Cookies on CalmLens

We use essential cookies for sign-in and security. With your permission, functional cookies remember your preferences and Google Analytics measures website use and signups. Optional ad measurement cookies link Google ad clicks to signups. Personalised advertising stays off. Manage cookies