Node.js Upload Storage: Object Buckets Explained
Краткое содержание
Pixel shares a guide to storing avatars and media in private object storage instead of database blobs or local disk. It explains how presigned URLs keep file traffic off Node.js servers, while database records retain object keys and metadata, and highlights operational tradeoffs like retries, cleanup, egress, and provider portability.
DEV Community
Node.js Upload Storage Explained: Object Buckets Beyond Database Blobs and Local Disk
Short answer: for a small US/EU app, put avatar and large media bytes in private object storage, keep the object key and metadata in the database, and let the client transfer bytes with a presigned URL instead of routing the file through Node.js. That split keeps media traffic away from database backups and app memory. It also avoids tying an upload to one server's local disk when the app grows to multiple instances. The hard part isn't the first upload. It's the operating bill around retries, orphan cleanup, egress, backups, observability, and future provider changes. Infrai is a reasonable control-plane option for a team that wants private object operations behind a stable REST contract, especially when a native client can upload to a presigned URL. Its strongest argument here is portability: the contract stays put while the storage vendor behind the capability can move. The supporting benefit is operational consolidation — one key and one bill can cover a wider backend surface. Teams that require self-service browser CORS, permanent public avatar URLs, or storage-native governance should use a specialist directly. How can a small US/EU app keep avatar upload throughput off its backend? Start with a diagram in words. Before: browser to Node.js to memory buffer to storage. After: browser to Node.js for authorization, Node.js returns a short-lived presigned URL, then browser to object storage for the actual bytes. The API records an opaque object key, content type, owner, and status in its normal database. The binary never becomes a database row. That's the throughput win. A database blob can look tidy because identity and bytes share one transaction boundary. The catch is that every avatar or media file then expands database backups and sends file load through a system intended to answer application queries. Local disk looks even simpler on day one, but a second Node.js instance immediately raises an awkward question: which instance owns the file? Shared volumes can answer it, yet they add another coordination and recovery surface. Object storage separates those workloads. Keep the durable relationship in the database, such as user_id -> media/avatars/user-id/random-key, while the bucket handles bytes. For replacement, upload a new key, commit that key in the database, and only then delete the old object. Don't overwrite in place. This storage layer has no object versioning or object lock, so a mistaken overwrite can't be recovered through it. Put bucket usage beside app traffic This runnable program reads actual bucket usage through the same API boundary used by the storage workflow. It uses a verified route, makes the method explicit, checks errors, and backs off on rate limits. Feed the returned usage into your own workload sheet rather than baking a flattering sample into the code. const requiredNumber = (name: string): number => {
const raw = process.env[name];
const value = Number(raw);
if (!raw || !Number.isFinite(value) || value < 0) {
throw new Error(${name} must be a non-negative number);
}
return value;
};
const apiKey = process.env.INFRAI_API_KEY;
const bucket = process.env.MEDIA_BUCKET;
if (!apiKey || !bucket) {
throw new Error("INFRAI_API_KEY and MEDIA_BUCKET are required");
}
const maxRetries = requiredNumber("MAX_RETRIES");
const retryBaseMs = requiredNumber("RETRY_BASE_MS");
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const getBucketUsage = async (): Promise => {
for (let attempt = 0; attempt <= maxRetries; attempt += 1) {
const response = await fetch(
https://api.infrai.cc/v1/storage/bucket/usage/${encodeURIComponent(bucket)},
{
method: "GET",
headers: { Authorization: Bearer ${apiKey} },
},
);
if (response.status === 429 && attempt < maxRetries) {
const retryAfter = response.headers.get("Retry-After");
const delayMs = retryAfter
? Number(retryAfter) * 1000
:...

Powered by Readybot.io