ফাইল ও মিডিয়া
object storage, presigned upload, metadata, CDN, scan আর lifecycle।
Start with the production incident, then the design decision that fixes it.
চলো-র rider প্রোফাইল ছবি প্রথমে Postgres-এর users.avatar bytea column-এ রাখা হতো। ৫ লাখ ছবির পর DB backup ৪০GB, query ধীর, আর একটা SELECT * যেকোনো admin page-এ ৫MB row টেনে আনছে। তারপর invoice PDF আসল—প্রতি ride-এ একটা, দিনে ৪০ হাজার। DB তো ভেঙেই পড়ল।
মাহিন বলল, "ছবি আর PDF ডেটাবেসের কাজ নয়।" ফাইল/মিডিয়া আলাদা object storage-এ রাখো—S3/R2/GCS; DB-তে শুধু metadata (key, mime, size, owner, checksum)। upload/download signed URL দিয়ে, CDN দিয়ে serve, lifecycle দিয়ে expire। এই টপিকের লক্ষ্য: ফাইল path-টা সঠিকভাবে আঁকা, যাতে DB হালকা আর file secure থাকে।
Object storage। Key → bytes; infinite-ish scale, 11 nines durability, HTTP API। Bucket public নয়; access IAM/presigned URL। Database-এ blob রাখলে row size, backup, replication সব খারাপ হয়।
Upload path। Client → API POST /uploads → server presigned PUT URL (৫ মিনিট) → client সরাসরি storage-এ upload → API confirm metadata। Server-এ multipart bytes টানো লাগে না; bandwidth বাঁচে।
Download path। Private file: API checks permission → presigned GET (১–৫ মিনিট) বা proxy stream। Public asset: CDN URL, long cache, immutable version key। Content-Disposition/mime ঠিক করো; XSS এড়াতে text/html serve নয়।
Metadata DB। files(id, owner_id, bucket, object_key, mime, size, checksum, status, created_at); unique key; index owner. Object key-তে user input নয়—random UUID; path traversal/injection এড়াও।
Validation। Size limit, allowed mime, magic-byte sniff (extension নয়), image dimension decode, virus scan (ClamAV) quarantine bucket। Upload URL-এ content-type/size condition বসাও।
Lifecycle। Temp upload ২৪ ঘণ্টায় delete; invoice ৭ বছর; avatar পুরোনো version ৩০ দিন। Delete = metadata soft-delete + object lifecycle; orphan object cleanup job।
| প্রশ্ন | সিদ্ধান্ত |
|---|---|
| Bytes কোথায়? | object storage |
| DB-তে? | metadata only |
| Upload? | presigned PUT |
| Private read? | auth check + presigned GET |
| Public read? | CDN + immutable key |
| Abuse? | size/mime/scan/quota |
Presigned upload আর metadata confirm:
def create_upload(user_id, filename, mime, size):
if size > 10 * 1024 * 1024 or mime not in {"image/png", "image/jpeg", "application/pdf"}:
raise BadRequest("unsupported file")
key = f"u/{user_id}/{uuid4()}"
url = s3.generate_presigned_url(
"put_object",
Params={"Bucket": BUCKET, "Key": key, "ContentType": mime},
ExpiresIn=300,
)
db.insert_file(key=key, owner=user_id, mime=mime, size=size, status="PENDING")
return {"uploadUrl": url, "key": key}
CREATE TABLE files (
id uuid PRIMARY KEY,
owner_id uuid NOT NULL,
object_key text NOT NULL UNIQUE,
mime text NOT NULL,
size_bytes bigint NOT NULL CHECK (size_bytes <= 10485760),
checksum text,
status text NOT NULL DEFAULT 'PENDING',
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX files_owner_idx ON files (owner_id, created_at DESC);
Confirm endpoint-এ HEAD object করে size/mime/checksum মিলিয়ে status READY করো; client-এর কথায় বিশ্বাস করো না।
১. File class করো। Avatar (public, small), invoice (private, PDF), chat image (private, medium), export (private, temporary)। প্রতিটার retention/access আলাদা।
২. Upload path। Presigned PUT + size/mime condition; server metadata PENDING; confirm-এ HEAD verify; virus scan async READY/QUARANTINE।
৩. Download path। Permission check → signed GET/proxy; public asset CDN immutable key; Content-Disposition: attachment যেখানে দরকার।
৪. Quota/abuse। Per-user ৫০ file/24h, ২৫০MB; rate limit; magic-byte; extension spoof আটকাও।
৫. Lifecycle। Temp/orphan cleanup, invoice retention, versioning; delete soft + object delete; audit।
লোকাল MinIO/S3 থাকলে aws s3 presign বা SDK দিয়ে ৫ মিনিটের URL বানাও; time expire হলে curl fail করে দেখো। এখন URL-এ Content-Type বদলে upload করলে fail করে কি না দেখো—signature-এ content-type বাঁধা থাকে।
Object key-তে filename/user input ব্যবহার করো না; random UUID। Signed URL log/commit করো না—URL-ই temporary credential। Virus scan fail হলে object quarantine bucket-এ সরাও, delete নয়; incident evidence থাকুক। Backup-এ object storage আলাদা policy; DB backup ফাইল ধরে না।
See the mental model first — the animation and the steps below it say the same thing.
Concept visual
ছবি object storage-এ রাখো, hot row-এ blob নয়—DB হালকা থাকে আর CDN সরাসরি পরিবেশন করে।
Write a short design on paper or in a note, then compare it with the rubric. This is not a judge submission.
Design practice
avatar public ২MB, invoice private PDF; DB-তে bytea রাখা হয়েছে।
One timed attempt. Solve every problem to earn the module badge.
Final checkpoint
Ready to prove this module?
Ask questions, share an approach or leave a study tip for this lesson.
Was this lesson helpful?
Your reaction helps other learners judge these notes.
0 comments
Sign in to ask a question or share an approach.
Sign in to commentNo comments yet
Be the first to ask a question or leave a study tip.
| প্রশ্ন | উত্তর |
|---|
| Storage? | S3/R2/GCS |
| DB? | metadata only |
| Upload? | presigned PUT |
| Private? | signed GET |
| Public? | CDN immutable |
| Abuse? | limit + magic + scan |