Delete Files from Amazon S3 Buckets in Spring Boot

Follow these steps to delete files from an Amazon S3 bucket using a REST API in Spring Boot:

  1. Create a Spring Boot Project:
  2. We assume you have a Spring Boot project set up and ready. If not, you can create one using Spring Initializr or your preferred approach.

  3. Add Amazon SDK Dependencies:
  4. Add the AWS Java SDK For Amazon S3 dependency to your Spring Boot project:

    <dependency>
        <groupid>com.amazonaws</groupid>
        <artifactid>aws-java-sdk-s3</artifactid>
        <version>1.12.556</version>
    </dependency>
    implementation group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.12.556'
  5. Add Configurations:
  6. To access S3 bucket from a Spring Boot project, you'll need to configure your project with AWS credentials. You can do this by providing your AWS access key and secret key, which can be set in your application.properties or application.yml file or loaded from environment variables. For example, in your application.properties file:

    server.port=8080
    
    aws.access-key = your-access-key
    aws.access-secret-key = your-access-secret-key
    aws.region = us-east-1
  7. Create an AmazonS3Client:
  8. Create a bean for the AmazonS3 client in your Spring Boot application configuration class. You can use the @Configuration annotation to create a configuration class:

    package com.example.app.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 AmazonS3Config {
    
      private String awsAccessKey;
      private String awsAccessSecretKey;
      private String awsRegion;
    
      public AmazonS3Config(@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();
      }
    
    }
  9. Create a Service:
  10. Create a service interface named S3FileDeleteService with a method for deleting files from your S3 bucket:

    package com.example.app.service;
    
    public interface S3FileDeleteService {
    
      void deleteFile(String fileId);
    
    }
  11. Create a Service Implementation:
  12. Create an implementation class named S3FileDeleteServiceImpl that implements the S3FileDeleteService interface and handles the business logic:

    package com.example.app.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import com.amazonaws.services.s3.AmazonS3;
    import com.example.app.service.S3FileDeleteService;
    
    @Service
    public class S3FileDeleteServiceImpl implements S3FileDeleteService {
    
      private String bucketName = "my-test-bucket";
    
      private String s3FolderName = "/myfolder/images/";
    
      @Autowired
      private AmazonS3 s3Client;
    
      @Override
      public void deleteFile(String fileId) {
        // get filename by id from your database
        String filename = "admission.pdf";
        String keyName = s3FolderName + filename;
        s3Client.deleteObject(bucketName, keyName);
      }
    
    }
  13. Create a Web Controller:
  14. Create a controller class named S3FileDeleteController. It will handle HTTP requests and interact with the S3FileDeleteService to delete files from your S3 bucket:

    package com.example.app.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.example.app.service.S3FileDeleteService;
    
    @RestController
    @RequestMapping(path = "/files")
    public class S3FileDeleteController {
    
      @Autowired
      private S3FileDeleteService s3FileDeleteService;
    
      @PostMapping(path = "/delete")
      public void deleteFile(@RequestParam(name = "fileId", required = true) String fileId) {
    
        s3FileDeleteService.deleteFile(fileId);
      }
    
    }