Hello Developers! 🚀
Today, I’m going to talk about how to integrate Amazon S3 with Spring Boot for efficient and secure file storage. Whether you're building a cloud-based application, handling user-generated content, or just looking for a scalable way to store files, AWS S3 is a reliable and cost-effective solution. In this article, I’ll guide you through setting up an S3 bucket, configuring access policies, integrating with Spring Boot, and handling file uploads/downloads using the AWS SDK. By the end, you’ll have a fully functional backend that interacts seamlessly with S3. Let’s get started!
AWS S3 Bucket Creation
General Configuration
1️⃣ AWS Region
Region: US East 1(N. Virginia) (us-east-1)
This means the bucket will be created in the US East (N. Virginia) data center.
Choose a region closest to your users to reduce latency and improve performance. Additionally, regions with multiple Availability Zones provide better redundancy and reliability. For most general-purpose applications, us-east-1 (N. Virginia) is a popular choice due to its low cost, high availability, and broad service support.
2️⃣ Bucket Type
General Purpose (Selected)
Recommended for most workloads.
Stores data across multiple Availability Zones for redundancy.
Directory (Not Selected)
Used for low-latency storage.
Supports S3 Express One Zone for faster access but no redundancy across multiple zones.
✅ Best Choice: General Purpose (default option, more reliable).
3️⃣ Bucket Name
Entered Name: spring-aws-s3
The bucket name must be globally unique across all AWS accounts.
AWS naming rules apply (lowercase, no spaces, no special characters except - and .).
4️⃣ Copy Settings from Existing Bucket (Optional)
Allows users to duplicate settings from an existing bucket.
"Choose Bucket" button enables selecting an existing bucket.
Not used in this case (default settings will be applied).
Configure Block Public Access Settings
Configure the Block Public Access settings to secure your bucket. You can allow public access for the Spring Boot application's REST API, customize these settings as shown below.
5️⃣ Allowing Public Access for REST API
Uncheck Block all public access and the related sub-options.
This setting will enable public access to your bucket, which is required for accessing files via your Spring Boot REST API.
Acknowledgement: Ensure you check the acknowledgment box to confirm the settings.
Warning: Allowing public access might expose your bucket contents. Use this option cautiously and consider adding restrictive bucket policies to limit access to specific API calls.
6️⃣ After configuring the settings, click on Create Bucket to finalize the process. Your bucket will now be ready to use!
Spring Boot Development
POM File Dependencies
To enable AWS S3 integration in your Spring Boot application, you need to add the required dependencies to your pom.xml file.
Note: Ensure that you use the correct versions of dependencies to avoid compatibility issues.
Configuration for AWS Credentials
Add the AWS credentials and region configuration in the application.properties file
AWS S3 Client Configuration
Create a configuration class to set up the S3Client for interacting with the S3 bucket. Here’s the code:
For more code details, you can refer to the public repository on GitHub: https://github.com/SachithMayantha/aws-s3-spring
Testing REST API with Postman
Once your Spring Boot application is running, you can test the file upload functionality using Postman. Configure your request as follows:
Method: POST
URL: http://localhost:8080/api/files/upload
Body: Select form-data and add a key file with the value set to the file you want to upload (e.g., Profile.pdf).
Same way, you can test other APIs as well.
Top comments (0)