Compressify takes a file, compresses it and gives it back. The first thing it does is accept an upload, and I built that the way most tutorials do: the browser posts the file to the API, the API receives it, and the API puts it into S3-compatible storage (MinIO).
It works, but it's also where the system does the most unnecessary work.
What the obvious way costs
In Compressify the upload handler reads the whole file into memory and then writes it to storage. Nginx in front allows uploads up to 500 MB. So every upload makes an API instance hold the whole file in RAM, and every byte crosses the network twice: browser to API, then API to storage.
Async I/O means this doesn't block a thread, so it's easy to think it's fine. But "non-blocking" isn't "free". The bytes still flow through the server's network card and memory buffers. Ten people uploading large videos at once is ten large files sitting in API memory. The more replicas you run to cope, the more you're paying to move bytes that never needed to touch the API.
Presigned URLs
S3 (and anything that speaks its API: MinIO, Cloudflare R2…) can sign a URL. The API, which has the storage credentials, creates a URL that allows exactly one thing: putting one object under one key, for the next few minutes. The browser uploads straight to storage with it.
const url = await getSignedUrl(
s3,
new PutObjectCommand({ Bucket: 'uploads', Key: `uploads/${userId}/${id}` }),
{ expiresIn: 300 }, // five minutes
);
The API's part shrinks to a small JSON request and response. Storage, which is built to take large uploads, takes them.
What I'd watch out for
- The server picks the key. The client asks for permission to upload; it doesn't get to choose where the file lands.
- Short expiry. Long enough for a slow upload, short enough that a leaked URL is useless. Minutes, not days.
- Store the key, not the URL. The signed URL expires. The object key is the thing to keep in the database.
- Confirm before you trust it. An upload URL being handed out doesn't mean a file arrived. Check the object exists (and its size and type) before you queue work on it.
What I took from it
Every hop a byte doesn't make is one you don't pay for, in memory, bandwidth or replicas. When a dedicated service already does the heavy lifting well, let the client talk to it directly, and keep your API for deciding who is allowed to.
Compressify is paused right now, but this is the first thing I'll change when I go back to it.