r/SpringBoot • u/codingwithaman Senior Dev • Jul 04 '26
Discussion Some of the rules I learned about file uploads based on my experience
I have built file upload features in a few projects over the years. Below are some rules that might save you some time.
- Never route files through your backend. Always generate a presigned url and let the client upload directly to s3. Let your backend just handles the permission check and stores metadata.
- Always validate file type by content not by extension. someone can rename virus.exe to photo.jpg and your system will accept it. Try to check the magic bytes server side. jpegs start with FF D8 FF, pngs start with 89 50 4E 47. Don't trust the extensions blindly.
- Use chunked uploads for anything above a few MBs. s3 supports multipart uploads natively. It can be done in spring boot also in multi part uploads.
split the file into 5-10mb chunks, upload independently, retry only the failed chunk. add resume tokens so users on flaky networks can continue from where they left off instead of restarting.
4) You should not serve files immediately after upload. every file should go through a scan queue first. virus scan passes then mark it ready. fails then quarantine it. Try to always have a gate between upload and access.
5)Don't expose raw s3 paths to users. Always generate signed download urls that expire in 15 minutes and then let backend checks user permissions before generating the url. Never share raw s3 links.
6) Always process everything async after upload. thumbnail generation, image compression, video transcoding, indexing...etc. none of this should block the upload response. return 201 immediately. let s3 events trigger workers in the background.
7) Set file size limits and rate limits based on your use case. Don't leave it open.
what rules do you follow for file uploads?
6
u/NewGuySham Jul 04 '26
Hmm nice advice, thanks man
6
u/BEgaming Jul 04 '26
Can you explain more the reasoning? Eg for point 1?
Is the file marked ready after the virus scan or only after all the async processing?
3
u/rlrutherford Senior Dev Jul 06 '26
The one criticism I have is this is more an AWS thing than Spring Boot; while there are applicable elements there, little is spring boot/java specific information. For instance use tika to check the uploaded file type.
-1
u/Kfct Jul 04 '26
I dont understand a lot of this. What is "route" through your backend from point 1? What is s3? How do you implement vague things like "chunk", "serve", "process async after upload"?