Video in progress
Where do you store files that users upload (images, PDFs, video), and how should Spring services work with them?
Files live neither in the database nor on the pod's disk but in object storage (S3, MinIO), with only the metadata in the database. The client uploads directly to storage with a presigned URL and downloads from it, and validation and processing happen afterward, asynchronously.
📖 Answer
Three common mistakes and what they lead to:
- BLOBs in the database: the database grows fast, backup and replication get slower, and the cache fills with useless file bytes.
- The pod's local disk: when the pod is recreated the files are gone, and a second pod can't see them.
- Everything flows through the application: uploading a 500 MB video ties up a Tomcat thread for minutes, and you pay for the network traffic twice.
The scheme I propose:
- The client asks the application for "upload permission". The service checks the rights, creates a record in the
PENDINGstate in the database and returns a short-lived presigned PUT URL (with limits on size and content type). - The client uploads the file straight to S3/MinIO, not through the application.
- A storage event (or the client's "done" call) triggers a processing queue: antivirus, checking the real file type (don't trust the extension), a thumbnail for images, transcoding for video. Only after that does the state become
READY. - Downloads go through a presigned GET URL or a CDN as well. For private files the URL lives for minutes; for public files use a CDN.
PresignedPutObjectRequest req = presigner.presignPutObject(b -> b
.signatureDuration(Duration.ofMinutes(10))
.putObjectRequest(p -> p.bucket(bucket).key(key).contentType(contentType)));
return new UploadTicket(fileId, req.url().toString());
Additional decisions:
- The object key is a UUID, not a name given by the user. The original name goes in metadata.
- Orphaned files: a background job that cleans up anything left in
PENDINGfor more than 24 hours, plus bucket lifecycle rules. - For personal documents use server-side encryption, the bucket is not public, and access goes only through signed URLs.
- Backup: versioning and cross-region replication of the object storage, planned together with the database backup, otherwise on restore the metadata and files won't match.
For local development and tests, use MinIO with Testcontainers: it has the same S3 API as production.
Sign up to mark studied questions and save your progress.