Validate file size during an API upload

Apply declared and measured upload-size gates, add SHA-256 integrity, and read the final measured byte size from Dosya’s stored-file receipt.

Last reviewed

Dosya checks the declared size before accepting bytes and measures the body while reading it. One-shot uploads require a decimal Content-Length; files above 50 MiB must use the upload-session flow.

Four workflow panels show measuring the source, selecting one-shot or resumable transfer, checking SHA-256, and verifying the stored size receipt.
The diagram covers measuring the source, selecting one-shot or resumable transfer, checking SHA-256, and verifying the stored size receipt. Open full-size screenshot.

Before you start

  • Check the API key’s max_file_size_bytes, the workspace storage quota, and the workspace max_file_size_gb setting. REST uploads do not apply a role-level file-size cap.
  • The key must reach the selected workspace, and the member role must allow upload_files plus access_upload for this REST upload surface.
  • Compute a lowercase 64-character SHA-256 digest when end-to-end byte identity matters.
  • Treat Content-Length as a gate, not proof of the stored size; the final receipt reflects bytes actually received.

Steps

  1. Measure the source before transfer. Use one-shot upload at or below 50 MiB; initialize a resumable upload above that boundary.
  2. For one-shot transfer, send a digits-only Content-Length. Missing or malformed length returns 411; an amount over 50 MiB returns 413 with Files over 50 MB use /api/upload/init and the resumable upload.
  3. Add X-Dosya-Sha256: digest when available. A mismatch returns 400 with hash_mismatch and expected and received values.
  4. Send the body while retaining the status and JSON error. A measured body that crosses the allowed buffer can return 413 with upload_declared_size_mismatch.
  5. Handle policy refusals literally: This API key does not permit files larger than N bytes (max_file_size_bytes), File exceeds the X GB size limit for this workspace, or the storage-quota response.
  6. On success, read the receipt’s actual stored size and file ID, then fetch metadata. Do not reject a successful upload merely because fewer bytes arrived than declared; decide against the measured result and digest.
Dosya developer overview with API and SDK entry points for validate file size during an api upload.
The developer overview supplies the API and SDK entry points referenced by “Validate file size during an API upload”. Open full-size screenshot.

Expected result

An allowed body produces a file receipt whose measured size matches metadata, and an optional SHA-256 confirms the bytes. Oversized, disallowed, or corrupt input fails before it becomes a trusted result.

Troubleshooting

The server returns 411. Supply the actual decimal byte length; chunked one-shot transfer does not replace that requirement.

The file is over 50 MiB. Use /api/upload/init, 10 MiB parts, and the completion route.

Declared and stored sizes differ. Treat the receipt as authoritative, compare the digest, and investigate the producer before continuing.

Was this helpful?

Loading helpfulness results…

Voting saves a necessary ballot cookie for up to 180 days so you can change your answer.

Questions and replies

Ask about the steps in this guide. Questions are reviewed before publication.

Keep account details, credentials and private files out of public questions. Use private support for those.

Loading questions…

    Sign in to your Dosya account to ask a question.

    Check the outcome before you retry

    Use these shared checks when a task needs a second look.