The File Upload Engineer builds systems that move files from client to storage safely and efficiently. The naive approach - uploading to your server then re-uploading to S3 - doubles bandwidth costs, adds latency, and makes your API server a bottleneck. The correct architecture uploads directly from browser to S3 using a presigned URL generated by your API server.
Direct-to-S3 Upload Architecture
// Step 1: Client requests a presigned URL from your API
// Your server creates the URL but never touches the file
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { v4 as uuid } from 'uuid';
const s3 = new S3Client({ region: 'us-east-1' });
export async function POST(req: Request) {
const { filename, contentType, userId } = await req.json();
// Validate file type server-side
const ALLOWED_TYPES = ['image/jpeg', 'image/png', 'image/webp', 'application/pdf'];
if (!ALLOWED_TYPES.includes(contentType)) {
return Response.json({ error: 'File type not allowed' }, { status: 400 });
}
const key = `uploads/${userId}/${uuid()}/${filename}`;
const command = new PutObjectCommand({
Bucket: process.env.S3_BUCKET!,
Key: key,
ContentType: contentType,
ContentLength: 10 * 1024 * 1024, // Max 10MB per presigned URL
// ACL: Keep private - serve via CloudFront with signed cookies
});
const presignedUrl = await getSignedUrl(s3, command, { expiresIn: 300 });
return Response.json({ presignedUrl, key });
}
// Step 2: Client uploads directly to S3 using the presigned URL
async function uploadFile(file: File) {
const { presignedUrl, key } = await fetch('/api/upload', {
method: 'POST',
body: JSON.stringify({ filename: file.name, contentType: file.type }),
}).then(r => r.json());
await fetch(presignedUrl, {
method: 'PUT',
body: file,
headers: { 'Content-Type': file.type },
});
// Step 3: Notify your server that the upload is complete
await fetch('/api/upload/complete', {
method: 'POST',
body: JSON.stringify({ key }),
});
}
Multipart Upload for Large Files (>100MB)
// S3 multipart: split file into 5MB+ chunks and upload in parallel
// S3 reassembles chunks server-side after all parts are uploaded
const CHUNK_SIZE = 10 * 1024 * 1024; // 10MB
async function multipartUpload(file: File, key: string) {
// 1. Initiate
const { uploadId } = await fetch('/api/upload/multipart/start', {
method: 'POST',
body: JSON.stringify({ key, contentType: file.type }),
}).then(r => r.json());
// 2. Upload parts in parallel (max 5 concurrent)
const parts = [];
const chunks = Math.ceil(file.size / CHUNK_SIZE);
for (let i = 0; i < chunks; i += 5) {
const batch = Array.from({ length: Math.min(5, chunks - i) }, (_, j) => {
const start = (i + j) * CHUNK_SIZE;
const chunk = file.slice(start, start + CHUNK_SIZE);
const partNumber = i + j + 1;
return uploadPart(uploadId, key, partNumber, chunk);
});
parts.push(...await Promise.all(batch));
}
// 3. Complete - S3 assembles the parts
await fetch('/api/upload/multipart/complete', {
method: 'POST',
body: JSON.stringify({ key, uploadId, parts }),
});
}
Security Checklist
- Validate MIME type server-side from magic bytes, not the
Content-Typeheader (clients can spoof it) - Scan uploaded files with ClamAV or a cloud service (VirusTotal API) before making them accessible
- Never serve uploaded files from your domain - use a CDN subdomain (
cdn.example.com) to prevent XSS via uploaded HTML files - Set S3 object ACL to private; serve all files via CloudFront with signed URLs or cookies
- Implement per-user upload quotas - track total bytes in DB, reject uploads that exceed the limit in the presigned URL request