Stream an API download to disk

Follow Dosya’s guarded download redirect without forwarding the API key, stream storage bytes to disk, and validate the saved file against metadata.

Last reviewed

The standard download route authorizes the request, then returns HTTP 302 to a five-minute presigned storage URL. The raw route stays on Dosya and supports byte ranges. These are distinct contracts; a 302 response is not itself a file stream.

Four workflow panels show recording file metadata, capturing the guarded 302 URL, streaming without the Dosya credential, and validating the byte count.
The diagram covers recording file metadata, capturing the guarded 302 URL, streaming without the Dosya credential, and validating the byte count. Open full-size screenshot.

Before you start

  • A read-scoped API key is sufficient, but the workspace role must expose the Files page and include download_files.
  • Know the file ID. Add version=N for a previous version or ut=token only when your flow received an unlock token.
  • Write to a temporary local path so an interrupted response never replaces a known-good file.

Steps

  1. Fetch metadata first and record the expected name and size. Refuse the job if the selected version or unlock context is not the one the caller intended.
  2. Request GET /api/files/file_id/download without automatic redirect handling. Require 302 and read Location; the signed URL normally expires after 300 seconds.
  3. Open Location as a separate request without Authorization or the Dosya API key. Stream response chunks into the temporary file instead of buffering the full object.
  4. Alternatively, call GET /api/files/file_id/download-url; its JSON contains url, size, name, and region. A requested TTL defaults to 300 seconds and is capped at 3600.
  5. For resumable byte transfer, use GET /api/files/file_id/raw with Range: bytes=start-. Expect 206 and Accept-Ranges; a range outside the object returns 416.
  6. After the stream closes, compare bytes written with metadata or the download-url size. Rename the temporary file into place only after validation.
Dosya developer overview with API and SDK entry points for stream an api download to disk.
The developer overview supplies the API and SDK entry points referenced by “Stream an API download to disk”. Open full-size screenshot.

Expected result

The client stores the complete object without exposing its API credential to the storage host. A full download matches the expected byte count; a resumed raw request returns the correct remaining range.

Troubleshooting

The first response is 302. That is the standard route’s success contract. Follow Location in a credential-free request.

The signed URL expired. Request a new URL after reauthorizing; do not edit its query string.

The raw request returns 416. Compare the local partial size with the current remote size. Restart when the partial file exceeds or belongs to another version.

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.