DEV Community

SilasFletcher5857
SilasFletcher5857

Posted on

Node.js 3-Stage User Document Storage Workflows for Large Media Uploads

Short answer: User document storage and large media uploads should use the same three-stage rule: authorize narrowly, transfer directly, and verify before committing application state. Pick a service only after testing multipart behavior and operational controls against the largest files; the interface name alone is not enough.

Upload large media files with a short-lived, client-to-object-storage path, then treat completion as an idempotent workflow rather than as a successful HTTP request. The three stages are authorization, direct transfer, and verification. This keeps the application out of the byte path while preserving the audit trail needed to recover from retries and interrupted uploads.

The constraint is throughput. A 4 GB video should not consume an application worker, its network connection, and its memory budget merely because the worker issued the upload request. The application should decide what may be uploaded; the storage service should carry the bytes.

The reader may start with a storage comparison for user documents, but the operational boundary is the same for a media service that accepts large files. The provider decision should follow the transfer contract, not lead it.

What fails when the app owns the byte path?

The failure signal is usually misleading: an upload endpoint returns 200, but the object is absent, incomplete, or attached to the wrong media record. A reverse proxy timeout can leave the client retrying while the first transfer is still finishing. A worker restart can strand an upload. A user can close the browser after the last byte moves but before the application records completion. These are different failures with the same unhappy result: the database and object store disagree.

I have been paged by missed jobs and duplicate deliveries, so I treat a completion callback as untrusted input until the durable state and the remote object agree.

I start with the state machine, not the SDK. For each upload, persist an internal upload ID, tenant or account ID, intended object key, expected size, content type, and state. created, authorized, transferring, verifying, complete, and aborted are enough for a first version. The state is the control plane; the object is data.

The object key should be generated by the service, never accepted as a path supplied by the browser. Keep the original filename as metadata. That decision prevents names such as ../../exports.csv from becoming storage paths, and it gives retries one stable destination.

A direct upload still needs boundaries. The authorization response should constrain the key, maximum size, content type policy, expiry, and the upload ID. It should not grant a general bucket credential. A signed request is an authorization artifact, not proof that the upload finished.

Which three stages make large-file transfer recoverable?

Stage one creates intent. The API authenticates the caller, checks quota and policy, generates the storage key, and records the expected size. It returns only the information needed to perform the transfer.

Stage two transfers bytes directly from the client to object storage. For a large object, use multipart upload when the storage API supports it. Multipart transfer lets a client retry a failed part rather than replaying the whole file. AWS documents this mechanism in its multipart upload overview; the same architectural idea applies when another service exposes a compatible multipart interface.

Stage three verifies completion. The client may notify the API, but the API must verify the storage-side result before changing the database state to complete. Check the object key, size, upload ID or version information available from the storage interface, and the expected account owner. A notification is a hint. Verification is the transition.

Here is the control-plane shape in Go. The storage interface is deliberately generic; it keeps application logic independent from one provider's client package.

type UploadIntent struct {
    ID          string
    AccountID   string
    ObjectKey   string
    ExpectedLen int64
    MediaType   string
    State       string
}

type Storage interface {
    CreateMultipart(ctx context.Context, key, mediaType string) (string, error)
    CompleteMultipart(ctx context.Context, key, uploadID string, parts []Part) error
    Head(ctx context.Context, key string) (ObjectInfo, error)
    AbortMultipart(ctx context.Context, key, uploadID string) error
}

type Part struct {
    Number int
    ETag   string
}

type ObjectInfo struct {
    Key         string
    Size        int64
    MediaType   string
}
Enter fullscreen mode Exit fullscreen mode

The missing detail in many implementations is the completion handler. It must be safe to call twice. On the first call, it verifies and commits. On a retry, it reads the upload record and returns the existing result if the same upload is already complete. If the caller presents a different upload ID for the same logical record, reject it. That is an integrity check, not an edge case.

Do not use the browser's reported size as the only validation. It is useful for early rejection, but storage-side metadata is the authoritative check after transfer. For media that needs stronger guarantees, calculate a checksum in the client or during an asynchronous inspection step, then compare it with the value the storage API exposes. Keep that check separate from the fast path if hashing a multi-gigabyte file would make completion feel hung.

How should user document storage work in 2026?

The names in the usual comparison are less important than the contract. AWS S3 documents multipart upload directly. Wasabi documents an S3-compatible API, and Backblaze B2 documents an S3-compatible API as well. Those statements describe an interface boundary; they do not prove identical lifecycle behavior, limits, metadata rules, or operational tooling. Test the exact calls your workflow needs.

For a user-document product, the question is often framed as a storage choice. For a media pipeline, the better question is: can this service sustain the part size, concurrency, retry pattern, checksum behavior, and completion semantics that the clients require? The answer belongs in a small compatibility test, not in a feature checklist.

Run the same test against each candidate using a fixed fixture set: a 20 MB file, a 4 GB file, an interrupted upload, a repeated completion request, and an object whose declared content type is intentionally wrong. Record elapsed transfer time, failed-part recovery, final object size, and the behavior of an abandoned upload. Do not turn one laptop's bandwidth into a provider benchmark. The test is for contract differences and failure handling. The trade-off is deliberate: a compatibility test takes engineering time, but skipping it moves that work into an incident with less evidence and worse recovery options.

Keep it boring.

A useful result table has these columns:

Check Expected result Failure response
Part retry Only the failed part is resent Keep upload open, retry with backoff
Repeated completion Same durable result Return the existing upload result
Wrong object size Completion is rejected Mark verification failed and alert
Expired authorization New transfer is denied Create a new intent; do not widen scope
Abandoned upload No database completion Abort or expire it through a reconciler

This is where a provider-neutral adapter earns its keep. It should expose the small set of operations the product needs, translate provider-specific errors into stable internal categories, and preserve the original error for logs. It should not pretend that all storage systems have the same consistency, lifecycle, or checksum behavior.

What do you verify after deployment?

Watch the workflow, not just the upload endpoint. The useful signals are authorization failures, bytes transferred by the client, incomplete multipart uploads, completion latency, verification failures, orphaned database intents, and the age of the oldest non-terminal upload. Break those metrics down by object size band. A 20 MB success rate can hide a broken 4 GB path.

Log an upload ID, account ID, object key hash, part number where relevant, and state transition. Avoid logging signed URLs or document contents. Correlate the client event, storage event if available, and verification request with the same internal ID. This makes a late callback diagnosable without exposing a credential that is still valid.

Use a reconciler for the boring cases. It finds intents stuck in authorized, transferring, or verifying beyond a policy window, checks storage, and either completes the record or aborts the remote multipart upload. Make the reconciler idempotent too. It may run twice after a deployment, and that should be harmless.

Rollback needs a boundary. If the new direct-upload path fails verification, stop issuing new authorizations and leave existing intents inspectable. Do not delete objects as a first reaction; an operator may need them to compare metadata or recover a valid transfer. After the incident window, expire only records that meet the documented retention rule.

The practical decision rule is narrow: choose the storage service whose tested multipart behavior, authorization model, lifecycle controls, and observability fit the largest files you actually accept. Keep the application responsible for intent and verification. Keep the bytes out of it.

References

Top comments (0)