Posts

Showing posts with the label Java S3 Example

Spring Boot on AWS EC2: Upload to S3 Securely Using IAM Role

When running a Spring Boot application on AWS EC2 , the most common security mistake is hardcoding AWS access keys inside application.properties . This is unnecessary and risky. AWS already provides a secure mechanism: IAM Roles attached to EC2 . In this blog, you will learn how to upload and download files to Amazon S3 from Spring Boot running on EC2 without any access keys , using: Spring Profiles AWS SDK v2 Instance Metadata Service (IMDS) IAM Roles Why Access Keys Are a Bad Idea on EC2 Typical S3 setups use: Access key Secret key Region Bucket This works only for local development. It should NOT be used on EC2. Problems with access keys: Stored in config or code (high risk) Might leak through Git, logs, or screenshots Require manual rotation Not needed if app runs on EC2 AWS automatically manages temporary credentials using the EC2 Instance Profile (IAM Role) . Architecture: EC2 → IAM Role → S3 Spring Boot app r...

Spring Boot + AWS S3: Upload and Download Files

Connecting a Spring Boot application to Amazon S3 is one of the most common real-world backend requirements. Whether you want to store images, documents, logs, or app data, S3 provides secure, scalable, and durable object storage. In this tutorial, you’ll learn how to integrate Spring Boot with AWS S3 using AWS SDK v2 to build clean and reliable upload/download APIs. High-Level Architecture The overall workflow is simple: Your Spring Boot application runs locally or on a server. The app uses AWS SDK v2 to create an S3Client . The S3Client connects to your S3 bucket using AWS credentials. REST APIs handle upload and download requests and interact with S3 internally. This setup works for local development, on-prem servers, and cloud deployments. Step 1: Create a Spring Boot Project Create a new project using Spring Initializr: Project: Maven Language: Java Spring Boot: 3.x+ Dependencies: Spring Web Java Version: 17+ Import the project ...