Blog

Sending large files to CalmLens one part at a time

Multipart uploads send a large file to CalmLens in ordered parts: how an upload opens, continues and ends, which limits apply and when moderation starts.

5 min read

  • Uploads
  • API
  • SDK

A single upload request works well for most images and documents. It works less well for a long video or a large PDF, where one interrupted request means sending the whole file again and the full payload has to sit in memory on your side. Multipart uploads let you send that file as a series of parts instead, each in its own request.

Three steps, two methods

A multipart upload has no separate "start" or "complete" call. The first part opens the upload, every later part continues it, and the part you mark as final closes it.

  1. Create the asset with the first part. This uses the same asset creation request as a normal upload, with a part object in place of content. The part carries partNumber: 1 and type, the MIME type of the whole file.
  2. Send each later part to the asset's part endpoint with the next partNumber.
  3. Set isFinal: true on the last part. CalmLens assembles the file and processes it.

In the SDKs, those steps map to creating the asset with its first part and then uploading the rest:

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,
  },
});

The first response is an asset whose upload.status is "uploading". Keep its id, because every later part refers to it. If a file other than an image fits in one part, you can send partNumber: 1 with isFinal: true in the first request and the upload opens and closes in one call.

Parts can be passed as base64 strings or byte arrays. If you use base64, split the original bytes first and encode each part separately. Cutting one long base64 string into pieces breaks the encoding at the boundaries, and CalmLens rejects a part that does not decode cleanly.

Parts arrive in order

Parts must be sent one at a time, in order, starting at 1. CalmLens keeps a count of the parts it has received on the asset itself, in upload.partsReceived, and the next part must be exactly one higher.

Because parts are sequential, that count is all the progress state you need. There are no gaps to track and nothing to reorder when the upload closes. If your process stops halfway through, retrieve the asset, read upload.partsReceived, and carry on from the following part.

Out-of-order requests fail with a 409 and a specific error code:

Error codeWhen it happens
upload-part/invalid-numberA part number is skipped or repeated. The error details include expectedPartNumber and receivedPartNumber.
upload/use-upload-partThe creation request carries a part number other than 1. Later parts belong on the part endpoint.
upload/not-openThe asset is no longer accepting parts, for example because the final part has already been received.

Limits

Every part except the last must be at least 5MiB. The final part can be any size. While an upload is open, its parts are held in object storage as a multipart object, and that storage only joins non-final parts of at least this size.

Non-image files can total up to 5,000 MiB, but audio and video processing has separate size and duration limits. If you already know the final size, pass totalSizeInBytes with the first request. CalmLens checks it against the limit before any data is stored, so an oversized file fails on the first part rather than the last.

The type on the first part has to be a concrete file type such as video/mp4 or application/pdf. Container types like application/octet-stream and multipart/form-data are rejected with upload/unsupported-type, because CalmLens can't tell how to process or store the content.

Images have their own limits. A single request takes an image of up to 10 MiB, and a larger image must be sent in parts, up to 20 MiB in total. Below that, an image can be sent in parts at any size, down to one final part. A larger image in one request, or a multipart image over 20 MiB, fails with upload/too-large, whose details.limitBytes gives the limit it exceeded. When an image upload is finalised, CalmLens may re-encode it to a more efficient format so the stored image stays within the image size limits.

When moderation starts

Nothing is classified while parts are arriving. The intermediate responses only update the asset's size and part count. Once the final part arrives, CalmLens assembles the parts into a single file, finalises the asset's content.format and content.checksums, sets classification.status to processing, and then hands it to classification in the same way as a single-request upload.

Choose wait when you send the first part. CalmLens saves that choice with the upload, so another client or a restarted process can finish it using only the asset ID. Later parts inherit the saved choice; an explicit wait on the final part overrides it.

  • With wait=false, the final request queues classification and returns before the decision. Use asset events or webhooks to learn when the report is ready.
  • With wait=true, the default for a new upload, supported synchronous processing finishes before the final response returns.

Audio and video always process asynchronously, even with wait=true. Their final upload response is not a moderation decision; keep the content unpublished until its moderation status is allowed.

Retention follows your processing options. If you haven't asked CalmLens to keep content after processing and you classify in the request, the assembled file can be removed from storage immediately, with classification running on the bytes already in memory. The asset record and its moderation results remain.

SDK support

The SDKs cover both halves of the flow: client.assets.create() accepts a part in place of content, and client.assets.uploadPart() sends each following part to the asset you created. Both validate a part before sending it, including the 5MiB minimum for non-final parts and the base64 boundary check, so a malformed part fails locally instead of after a network round trip.

With the JavaScript and Python SDKs you rarely need to send the parts yourself. When you pass content larger than the client's part size (8 MiB by default, partSizeBytes to change it), assets.create and assets.classify split it, send each part with its own idempotency key, retry a part that fails, and return the finished asset. Calling the upload again with the same idempotencyKey resumes it from the first part CalmLens doesn't have.

Read the multipart uploads guide for request examples and the full list of part options.

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