PAU OJ Online Judge
ProblemsLearnStudy plansContestsSubmissionsRank

Updates

Announcements and upcoming contests

Loading announcements…
Sign in for personal alertsBrowse contests
Log inSign up

PAU OJ

PrimeAsia University Online Judge

A real online judge for programming practice, university contests and coding assessments. Write code in your browser, get sandboxed verdicts, and track your progress.

Platform

  • Problems
  • Contests
  • Submissions
  • Ranking
  • Reviewers

Resources

  • User guide
  • Changelog
  • How judging works
  • System health
  • Create an account

© 2026 PrimeAsia University Online Judge. All rights reserved.

v0.9.0

  1. All tracks
  2. Master class
  3. Files & Media

Files & Media

ফাইল ও মিডিয়া

Topic 20 of 41Story · visual · design practice

In this lesson

  • Story & concept
    • কেন দরকার
    • মূল ধারণা
    • কোডে দেখি
    • ধাপে ধাপে
    • সাধারণ ভুল
      • ল্যাব: presigned URL মাপো
      • চিট শিট
      • প্রোডাকশন নোট
  • Visual story
  • Design practice
  • Checkpoint
  • Discussion
Back to track

0/0 solved

object storage, presigned upload, metadata, CDN, scan আর lifecycle।

In this lesson

  • Story & concept
  • Visual story
  • Design practice
  • Checkpoint
  • Discussion
01

Step 1: Story & concept

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।

সাধারণ ভুল

  • ছবি/PDF DB-তে bytea রাখা। Row/backup/replication explode; object storage ব্যবহার করো।
  • Public bucket। Private file-এ IAM/presigned URL; bucket policy audit করো।
  • Client-এর filename/path দিয়ে object key। Path traversal/injection; random UUID।
  • শুধু extension দেখে mime trust। Magic-byte sniff + size/dimension decode করো।
  • Signed URL log/commit। URL temporary credential; log-এ redact।
  • Lifecycle ছাড়া orphan object। Temp upload/old version storage খেয়ে ফেলবে; cleanup job রাখো।

ল্যাব: presigned URL মাপো

লোকাল 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 ফাইল ধরে না।

02

Step 2: Visual story

See the mental model first — the animation and the steps below it say the same thing.

Concept visual

ফাইল S3-এ, পথ DB-তে

ছবি object storage-এ রাখো, hot row-এ blob নয়—DB হালকা থাকে আর CDN সরাসরি পরিবেশন করে।

tier 0

client
ক্লায়েন্ট

ছবি আপলোড

tier 1

API
সার্ভিস

presigned URL

tier 2

S3 bucket
স্টোরেজ

২MB original

CDN edge
edge

thumbnail 200KB

tier 3

scan worker
ওয়ার্কার

virus scan

metadata DB
ডেটাবেস

key আর owner

presigned S3 → CDN
  1. 1presigned URL দিয়ে আপলোড৫ মিনিটের টোকেন, সার্ভার মাঝখানে নেই
  2. 2key DB-তে রাখোowner, mime, size, checksum
  3. 3CDN দিয়ে পরিবেশন করোএকই ছবি বারবার origin-এ যায় না
  4. 4scan আর quota বসাও10MB সীমা, ভাইরাস স্ক্যান async
  5. 5lifecycle ঠিক করো৯০ দিনে cold tier, ১ বছরে delete
5টি ধাপে দেখা হচ্ছেগতি কমালে স্থির
03

Step 3: Design practice

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 ও invoice

20 min

avatar public ২MB, invoice private PDF; DB-তে bytea রাখা হয়েছে।

তোমার কাজ

  1. 1object storage + metadata schema design করো।
  2. 2presigned upload/download flow আঁকো।
  3. 3scan/quota/lifecycle লেখো।

ভালো উত্তরে কী থাকবে

  • DB-তে metadata only
  • presigned flow secure
  • scan/quota/lifecycle আছে
04

Step 4: Checkpoint

One timed attempt. Solve every problem to earn the module badge.

Final checkpoint

Ready to prove this module?

45 minutes3 problemsScale Architect badge
Go to checkpoint
05

Step 5: Discussion

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.

Sign in to react

Discussion

0 comments

Sign in to ask a question or share an approach.

Sign in to comment

No comments yet

Be the first to ask a question or leave a study tip.

Topic 20 of 41

PreviousTransactions & Isolationট্রানজেকশন ও আইসোলেশনTopic 19 of 41
NextREST, RPC & GraphQLREST, RPC ও GraphQLTopic 21 of 41
Back to track: System Design MasterclassAll learning tracks
প্রশ্নউত্তর
Storage?S3/R2/GCS
DB?metadata only
Upload?presigned PUT
Private?signed GET
Public?CDN immutable
Abuse?limit + magic + scan