この記事で学んだことをメモる。
Patterns
S3 における Signature Verification (署名検証)
S3 presigned URL / CDN signed URL のそれぞれを以下にまとめた。
HTTP range header
todo
DD
"What if the upload fails at 99%?"
-> Use S3 multipart upload.
(100MB 以上で推奨。各partialは 5MB 程度。 S3 SDKでは内部でやってくれてる。)
Design Dropbox - DD1: How can you support large files? が詳しい。
🟢 how will we handle resumable uploads?
Chunk ごとのアップロード状態を保持しておく。
After the disconnection, the client calls ListParts API in S3, and confirm which parts have already been uploaded. Then it resumes uploading the missing partials.
🟢 how should we ensure this chunks field is kept in sync with the actual chunks that have been uploaded?
checksumを使う。Chunk ごとに checksum を使って破損検知を行う。
🟢 アップロードの完了検知
クライアント側が全てのアップロードが完了したら、CompleteMultipartUpload API をコールする。
S3側で、本当に全てのアップロードが終わっているかを確認する。(= Trust but Verify)
アプリ側のメタデータに upload_status (pending, failed, succeeded) を持っておき、S3 event notificaiton を利用して、ファイル全体のアップロード完了後に書き換える。S3 --CDC--> Worker ---> PostMetadata。
🟢 アップロードが途中で失敗終了した場合のS3内のゴミの後片付け
📝 Checksum vs Fingerprint
DD
"How do you prevent abuse?"
abuse = harmful/unintended ways like:
- uploading malicious files like malware
- uploading inappropriate content
- uploading his personal irrelevant files (and lying about the file type)
- uploading huge files (like TB) to drive up the storage cost
🟢 解決策1 Processing pipeline for file checks
Implement a processing pipeline
- Client uploads a file to a private quarantine bucket first.
- Run a job the file conntent check. If fails, mark it as
REJECTEDstatus.- virus scans
- inappropriate content detection
- file type validation
- size checks
- Run a job to move the file to the public bucket, and update the database status to
AVAILABLE.
As a bonus, this approch prevents someone from uploading malicious content and immediately sharing the link.
🟢 解決策2 File size limit in presigned URL
max file size limit を指定して、presigned URL を作る。
DD
"How do you handle metadata?"
Primary DB の中に metadata table を作り、そこに保存する。
-
upload_statusは、PENDING,UPLOADED,REJECTED,AVAILABLEをとる。 - ファイルの object key は、
uploads/{user_id}/{timestamp}/{uuid}にする。- これにより、CDC の Worker は CDC から、どのメタデータレコードの status を更新すればいいかが分かる。
📝 conditional update はとばした。
DD
"How do you ensure downloads are fast?"
🟢 解決策1. CDN を使う
CDN からファイルをここからダウンロードすれば良い。
private content に関しては、Signed CloudFront URL を使う。
TODO: Cache Header の指定
🟢 解決策2. HTTP Range Requests を使う
HTTP Range Requests を使う。
(S3 SDKでは内部でやってくれてる。)
これにより、concurrent & resumable downloads が可能。




