How to Delete Files from Amazon S3 Buckets in Spring Boot
In this example, we will show how to delete a file from an Amazon S3 bucket using a REST API in Spring Boot.
Follow the steps below to complete this example:
Adding Dependency
To upload files to S3, you will need to add the AWS Java SDK For Amazon S3 dependency to your application. Here is the Maven repository for Amazon S3 SDK for Java.
Gradle DependencyAdd the following dependency to the build.gradle file:
implementation group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.12.158'
Maven Dependency
Add the following dependency to the pom.xml file:
Add Configurations
First, add the following credentials to your resources/application.properties configuration file:
server.port=8080
aws.access-key = your aws access key here
aws.access-secret-key = your aws secret key here
aws.region = us-east-1
Create Configuration Class
Create a configuration Java class for the AmazonS3 Client:
package com.s3.sample.demo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
@Configuration
public class AwsConfig {
private String awsAccessKey;
private String awsAccessSecretKey;
private String awsRegion;
public AwsConfig(@Value(value = "${aws.access-key}") String awsAccessKey,
@Value(value = "${aws.access-secret-key}") String awsAccessSecretKey,
@Value(value = "${aws.region}") String awsRegion) {
this.awsAccessKey = awsAccessKey;
this.awsAccessSecretKey = awsAccessSecretKey;
this.awsRegion = awsRegion;
}
public AWSStaticCredentialsProvider getAwsCredentialsProvider() {
BasicAWSCredentials awsCred = new BasicAWSCredentials(this.awsAccessKey, this.awsAccessSecretKey);
return new AWSStaticCredentialsProvider(awsCred);
}
@Bean
public AmazonS3 getAmazonS3Client() {
return AmazonS3ClientBuilder.standard().withRegion(this.awsRegion).withCredentials(getAwsCredentialsProvider())
.build();
}
}
Create Service
Create a service class with a method to delete a file from an Amazon S3 bucket:
import com.amazonaws.services.s3.AmazonS3;
public class S3DeleteServiceExample {
private String bucketName = "my-test-bucket";
private String s3FolderName = "/myfolder/images/";
@Autowired
private AmazonS3 s3Client;
public void uploadMultipleFileToS3(String fileId) {
// get filename by id from your database
String filename = "admission.pdf";
String keyName = s3FolderName + filename;
s3Client.deleteObject(bucketName, keyName);
}
}
Create Web Controller
Create a controller class with a REST API endpoint to delete a file from an S3 bucket:
package com.s3.sample.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.s3.sample.demo.service.S3DeleteServiceExample;
@RestController
@RequestMapping(value = "/api/files")
public class DeleteFileExampleController {
@Autowired
private S3DeleteServiceExample s3DeleteServiceExample;
@PostMapping(value = "/delete")
public void uploadMultipleFiles(@RequestParam(name = "fileId", required = true) String fileId) {
s3DeleteServiceExample.uploadMultipleFileToS3(fileId);
}
}
The code is complete. You can run and test your application.