B14 · 檔案與儲存 詳細 ROADMAP
計畫文件,不會被 Quartz 渲染。
回主 roadmap → backend/ROADMAP.md
章節目標
檔案上傳 / 下載 / 儲存聽起來簡單,實戰超多坑:大檔案上傳 OOM、timeout、MIME 偽造、儲存成本失控、CDN 配合。本章涵蓋 multipart / streaming / chunked 上傳、S3 / MinIO / pre-signed URL / CDN 整合。
🌱 基本介紹
| # | 主題 | Slug | Stage | 大綱 |
|---|
| 01 | 後端儲存是什麼 | 01-what-is-backend-storage | 🌱 | 本地檔案系統 / 網路共享 / 物件儲存的定位與用途 |
❓ 為什麼需要
| # | 主題 | Slug | Stage | 大綱 |
|---|
| 02 | 為什麼不該讓應用伺服器存檔案 | 02-why-not-local-fs | 🌱 | Horizontal scaling 失敗、disk 爆滿、備份困難、K8s Pod 重啟檔案不見 |
| 03 | 為什麼 client 直傳 S3 比經過後端好 | 03-why-client-direct-upload | 🌱 | 後端 bandwidth 吃不消、timeout、OOM;pre-signed URL 解法 |
🕰️ 演進
| # | 主題 | Slug | Stage | 大綱 |
|---|
| 04 | 物件儲存演進 | 04-object-storage-evolution | 🌱 | FTP → NFS → S3(2006)→ MinIO / R2 / B2 cost optimization 時代 |
🧠 知識型
F14-A 上傳策略
| # | 主題 | Slug | Stage | 大綱 |
|---|
| 05 | 檔案上傳總覽 | ⛔️ micro-service/25-file-upload-overview | 🌿 | 跨系列 |
| 06 | Multipart 上傳 | 06-multipart-upload | 🌱 | form-data 格式、field parser、memory vs disk buffer |
| 07 | Streaming 上傳 | 07-streaming-upload | 🌱 | 不 buffer 整個檔、直接 pipe 到 S3;各 framework 實作 |
| 08 | Chunked / Resumable Upload | 08-chunked-resumable | 🌱 | 大檔分塊、斷點續傳、tus.io 協議 |
| 08-2 | 多檔 / 批次處理極限狀態 | 08-2-batch-file-limit | 🌱 | 同時上傳 100+ 檔的瓶頸:memory / disk IO / concurrent upload 限制;分批策略、streaming zip、partial failure 恢復;跟 Queue 整合做背景批次處理;S3 multipart upload 並行 part 的限制 |
F14-B 物件儲存
| # | 主題 | Slug | Stage | 大綱 |
|---|
| 09 | S3 / MinIO / R2 / B2 | 09-object-storage-comparison | 🌱 | API 相容性、pricing 模型、egress fee(R2 免費 vs S3 高) |
| 10 | Pre-signed URL 策略 | 10-presigned-url | 🌱 | 上傳 / 下載 / 有效期限 / policy;前後端分工 |
| 11 | 儲存類型選擇 | 11-storage-class-selection | 🌱 | S3 Standard / IA / Glacier;lifecycle policy;成本優化 |
F14-C 媒體處理
| # | 主題 | Slug | Stage | 大綱 |
|---|
| 12 | 圖片處理 | 12-image-processing | 🌱 | resize / format(WebP / AVIF)/ watermark;on-the-fly(Imgix / Cloudinary)vs pre-generated |
| 13 | 影片處理 | 13-video-processing | 🌱 | transcoding(FFmpeg / AWS MediaConvert)、HLS / DASH streaming |
| 14 | CDN 整合 | 14-cdn-integration | 🌱 | origin / edge / cache policy;跟 storage 的邊界 |
F14-D 安全性
| # | 主題 | Slug | Stage | 大綱 |
|---|
| 15 | MIME 驗證 | 15-mime-validation | 🌱 | Content-Type header 不可信、magic number 驗證、副檔名欺騙 |
| 16 | 大小限制 | 16-size-limit | 🌱 | Application / Reverse proxy / Cloud provider 各層限制 |
| 17 | Access Control | 17-access-control | 🌱 | Public / Authenticated / Pre-signed 存取模式 |
| 17-2 | 儲存選型總結 | ⛔️ micro-service/66-conclusion-storage | 🌿 | 跨系列 |
F14-E 微服務化的 Storage 策略
| # | 主題 | Slug | Stage | 大綱 |
|---|
| 17-3 | Bucket 隔離策略 | 17-3-bucket-isolation | 🌱 | Per-service bucket vs 共用 bucket;命名 convention;監控 / 成本分帳 |
| 17-4 | 跨 Service 共用資源 | 17-4-cross-service-shared-assets | 🌱 | User avatar 要給多 service 看;owner service vs consumer service;API 設計(direct 存取 vs 透過 service) |
| 17-5 | Pre-signed URL 跨服務產生 | 17-5-cross-service-presigned | 🌱 | 哪個服務有 IAM 權限發 token;delegation 模式;security boundary |
| 17-6 | IAM / Bucket Policy 跨服務 | 17-6-iam-cross-service | 🌱 | 最小權限原則;role chaining;policy 管理(Terraform / Pulumi) |
| 17-7 | Cross-service File Lifecycle | 17-7-cross-service-lifecycle | 🌱 | User 刪帳號 → 哪些 service 的檔案要清;event-driven cleanup;orphan 檔案偵測 |
| 17-8 | Storage Tenant Isolation | 17-8-storage-tenant-isolation | 🌱 | 多租戶檔案隔離;prefix / separate bucket / encryption key per tenant |
🔧 小實作注意事項
| # | 主題 | Slug | Stage | 大綱 |
|---|
| 18 | 本地開發用 MinIO | 18-local-minio | 🌱 | Docker Compose 起 MinIO、跟 S3 SDK 共用 |
| 19 | 檔案上傳完整流程(從前端到 S3) | 19-upload-e2e | 🌱 | 前端 → API 要 pre-signed → 直傳 S3 → webhook 通知 → DB 寫入 |
💣 Anti-pattern
| # | 主題 | Slug | Stage | 大綱 |
|---|
| 20 | 儲存 Anti-patterns | 20-storage-antipatterns | 🌱 | 檔案存 DB(blob)大於幾 KB、本地 disk 存 user upload、MIME 只信 header、沒設 lifecycle policy 成本爆、pre-signed URL 有效期訂 7 天、client 直傳沒做權限驗證、把圖片 base64 塞 JSON 吃頻寬 |
🧰 對應檢查工具
| # | 主題 | Slug | Stage | 大綱 |
|---|
| 21 | 儲存相關工具 | 21-storage-tooling | 🌱 | aws s3 cli / mc(MinIO)、s3cmd、rclone、CloudBerry、bucket size 工具 |
📎 補充
| # | 主題 | Slug | Stage | 大綱 |
|---|
| S01 | Virus Scan 整合 | s01-virus-scan | 🌱 | ClamAV、上傳後非同步掃描、quarantine bucket |
| S02 | 檔案 dedup / 內容定址 | s02-content-addressable | 🌱 | hash 當 key(git / IPFS 模式)、節省儲存 |
章節進度統計
- 知識主題:21 + 2 補充 = 23 項
- 🌿 growing:1(跨系列)
- 🌱 seed:22
跨系列連結
- →
micro-service/25-file-upload-overview
- →
infra/10-storage-management、28-backup-disaster-recovery
- →
backend/api-design/ B09(upload API 設計)
- →
backend/security/ B16(MIME / virus)
- →
frontend/application/ CH8 F10(前端上傳 UI)