Category: spring_boot
Upload Files from Spring Boot to AWS S3
Published on 15 Sep 2026
Explanation
AWS S3 stores files inside buckets. A bucket can contain images, PDFs, videos, reports, and other objects.
Code:
AWS S3 Bucket ├── profile-images/ ├── invoices/ ├── resumes/ └── documents/
Explanation
Add the AWS SDK dependency to communicate with Amazon S3 from a Spring Boot application.
Code:
<dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3</artifactId> </dependency>
Explanation
Create an S3 client using the AWS SDK. Credentials should be provided through IAM roles, environment variables, or the AWS credentials provider chain.
Code:
S3Client s3Client = S3Client.builder()
.region(Region.AP_SOUTH_1)
.build();
Explanation
Upload a MultipartFile to an S3 bucket by creating a PutObjectRequest and sending the file bytes.
Code:
public String uploadFile(MultipartFile file)
throws IOException {
PutObjectRequest request = PutObjectRequest.builder()
.bucket(bucketName)
.key(file.getOriginalFilename())
.contentType(file.getContentType())
.build();
s3Client.putObject(request,
RequestBody.fromBytes(file.getBytes()));
return file.getOriginalFilename();
}
Explanation
Expose a REST endpoint that accepts a file from the client and sends it to the S3 service.
Code:
@PostMapping("/upload")
public String upload(@RequestParam MultipartFile file)
throws IOException {
return s3Service.uploadFile(file);
}