
Your AWS S3 Bucket Might Be Exposing More Than Just Files!
A practical guide to securing file uploads in AWS using Amazon S3, IAM, and Presigned URLs. Imagine you're building an application where users can upload profile pictures, identity documents, invoices, or payment receipts.
Your AWS S3 Bucket Might Be Exposing More Than Just Files! š
A practical guide to securing file uploads in AWS using Amazon S3, IAM, and Presigned URLs.
Imagine you're building an application where users can upload profile pictures, identity documents, invoices, or payment receipts.
You decide to use Amazon S3 because it's scalable, reliable, and easy to integrate with your backend.
You create an S3 bucket, connect it to your Spring Boot application, and successfully upload your first file.
Everything works perfectly.
But here's a question:
What happens if someone discovers the URL of another user's private document?
Can they access it?
Can they upload malicious files?
Can they overwrite existing files?
This is where cloud security becomes more important than simply making your application work.
Let's understand how to build a safer file-upload architecture using AWS.
1. Why Public S3 Buckets Are Dangerous
Amazon S3 makes storing and retrieving files incredibly convenient.
However, convenience should never come at the cost of security.
Imagine an application storing:
- User identity documents.
- Personal photographs.
- Financial statements.
- Business invoices.
- Private application documents.
If your bucket is publicly accessible, unauthorized users might be able to retrieve objects.
Even worse, incorrectly configured permissions could allow unauthorized uploads or modifications.
The first security principle should be:
Your application's private files should remain private by default.
For most private application storage, you should:
- Enable S3 Block Public Access.
- Avoid public bucket policies.
- Use least-privilege IAM permissions.
- Enable appropriate encryption.
- Restrict access to authorized users.
2. Stop Giving AWS Credentials to Your Frontend
This is one of the biggest mistakes developers can make.
Imagine putting AWS access keys directly inside your Android application or frontend JavaScript.
Anyone who extracts those credentials could potentially misuse the permissions associated with them.
Never expose long-term AWS credentials in client-side applications.
Instead, use your backend to authorize file access.
A safer architecture looks like this:
Client ā Spring Boot Backend ā AWS S3
Your backend verifies the user's identity and determines whether they are allowed to upload or download a particular file.
But there's an even better approach for transferring files.
3. Meet Presigned URLs
Amazon S3 supports presigned URLs.
A presigned URL provides temporary permission to perform a specific operation on an S3 object.
For example, your backend can generate a URL that allows a user to upload a file without exposing AWS credentials.
The workflow:
- User requests permission to upload a file.
- Spring Boot validates the user's authentication.
- Backend checks whether the user is authorized.
- Backend generates a presigned URL.
- Client uploads the file directly to S3.
- Backend stores the object metadata.
The best part?
Your backend doesn't need to handle the entire file transfer.
This reduces unnecessary traffic through your application server.
4. Implementing Presigned URLs with Spring Boot
AWS SDK for Java provides support for generating presigned URLs.
For example:
@Servicepublic class S3Service {
private final S3Presigner presigner;
public S3Service(S3Presigner presigner) { this.presigner = presigner; }
public String generateUploadUrl(String key) {
PutObjectRequest objectRequest = PutObjectRequest.builder() .bucket("my-private-bucket") .key(key) .contentType("image/jpeg") .build();
PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder() .signatureDuration( Duration.ofMinutes(5) ) .putObjectRequest(objectRequest) .build();
PresignedPutObjectRequest signedRequest = presigner.presignPutObject(presignRequest);
return signedRequest.url().toString(); }}This is a simplified example.
In production, you should also validate file types and sizes, generate unpredictable object keys, and ensure that users cannot overwrite another user's objects.
Also, remember that the client must upload using the request method and signed headers expected by the presigned request.
5. Use IAM Instead of Hardcoding Permissions
IAM stands for Identity and Access Management.
It allows you to control which AWS resources an application or user can access.
For example, your backend might only need permission to:
- Upload objects to a specific bucket.
- Retrieve specific objects.
- Delete objects when authorized.
You should avoid giving your application unrestricted access to every S3 bucket in your AWS account.
Use IAM roles and temporary credentials wherever possible.
For workloads running on AWS, services such as EC2 and ECS can use IAM roles to obtain credentials without embedding long-term access keys in application code.
Remember: Give every service only the permissions it actually needs.
6. Don't Forget Encryption
Securing access is only one part of protecting files.
You should also consider encryption.
Amazon S3 supports server-side encryption options, including:
- SSE-S3.
- SSE-KMS.
- DSSE-KMS.
These options help protect stored objects at rest.
For sensitive applications, SSE-KMS can provide additional control over encryption keys through AWS Key Management Service.
You should also use HTTPS when transferring files.
Encryption at rest and encryption in transit solve different problems, and both matter.
7. What About Malicious File Uploads?
Here's another problem.
A user might upload a file disguised as an image.
For example:
Ā
profile.jpg.exeĀ Simply checking the file extension is not enough.
A safer upload pipeline should include:
- File size validation.
- MIME-type validation.
- File signature inspection.
- Randomized object keys.
- Malware scanning where appropriate.
- Restricted download permissions.
For applications handling sensitive documents, you can introduce an asynchronous scanning workflow.
For example:
S3 Upload ā Event Notification ā SQS ā Scanning Worker ā Approved/Rejected
This separates file scanning from the main API request.
8. My Recommended Architecture
For a Spring Boot application hosted on AWS, I'd consider the following architecture:
Frontend / Android App
ā
Spring Boot Backend
ā
Authentication + Authorization
ā
Presigned URL Generation
ā
Amazon S3 Private Bucket
ā
S3 Event Notification ā SQS ā File Scanning Worker
ā
Database stores file metadata
This architecture separates responsibilities and avoids routing every file byte through your backend.
However, remember that S3 upload success does not automatically mean a file has passed security checks.
Your application should track file-processing status and prevent users from accessing files until they are approved.
Final Thoughts
Cloud security isn't something you add after completing your application.
It should be part of your architecture from the beginning.
Amazon S3 makes file storage easy, but developers are responsible for configuring access, permissions, encryption, and validation correctly.
You don't need a complicated security architecture to get started.
Start with private buckets, least-privilege IAM roles, temporary presigned URLs, and proper file validation.
Then introduce additional security controls as your application grows.
One final question:
If you were building a document-upload system, would you upload files through your Spring Boot backend or use presigned URLs to upload directly to S3?
I'd love to hear how other developers approach this architecture.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article