Welcome readers, in this tutorial, we will show how to delete a file from an AWS S3 bucket using the spring boot framework.
- Spring boot is a module that provides rapid application development feature to the spring framework including auto-configuration, standalone-code, and production-ready code
- It creates applications that are packaged as jar and are directly started using embedded servlet container (such as Tomcat, Jetty or Undertow). Thus, no need to deploy the war files
- It simplifies the maven configuration by providing the starter template and helps to resolve the dependency conflicts. It automatically identifies the required dependencies and imports them in the application
- It helps in removing the boilerplate code, extra annotations, and XML configurations
- It provides a powerful batch processing and manages the rest endpoints
- It provides an efficient jpa-starter library to effectively connect the application with the relational databases
- It offers a Microservice architecture and cloud configuration that manages all the application related configuration properties in a centralized manner
1.1 AWS Storage Service
AWS Storage Service or simply known as AWS S3 is an online storage facility for the users. It cheap, easy to set up and the user only pays for what they utilize. It offers,
- To host static web-content and data or even the dynamic pages
- Data storage for analytics
- Backup and archival of data
- Disaster recovery solutions
1.1.1 S3 Bucket
Amazon S3 Bucket has two primary entities i.e. Object and Bucket, where objects are stored inside the buckets. It provides high availability and durability solutions by replicating the data of one bucket in multiple data centers. Each AWS account offers 100 buckets as free, however, this count can be increased on submitting a request to the support center. Amazon S3 features –
- Each object in a bucket is given a unique id
- S3 allows a developer to upload/delete or read an object via the REST API
- S3 offers two read-after-write and eventual consistency models to ensure that every change command committed to a system should be visible to all the participants
- Objects stored in a bucket never leave it’s location unless the user transfer it out
- Objects can be made private or public and rights can be granted to specific users
1.1.2 Setting up AWS S3 bucket and IAM user
If someone needs to go through the process of creating an S3 bucket and attach it to an IAM user, please watch this video.
To start with this tutorial, we are hoping that users at present have their AWS account created and know how to set up an S3 bucket and attach it to an IAM user of their choice.
2. Spring boot and AWS S3: Delete file
Here is a systematic guide for implementing this tutorial.
2.1 Application pre-requisite
To start with this tutorial, I would recommend readers to go through Part 1 of Spring Boot and AWS S3 operations tutorial available at this link. The tutorial will help users to understand the code changes required to start up.
3. Creating a Spring Boot application
Since the Project structure, Maven dependencies, and Configuration level changes remain the same, so we’ll directly jump start with the changes required to delete the file from the S3 bucket.
3.3 Java Classes
Let us write the updated java classes involved in this application.
3.3.1 Service Class
deleteFile(……) method in the
AWSS3ServiceImpl.java class to delete the object from the S3 bucket.
3.3.2 Controller Class
deleteFile(……) method in the
AWSS3Ctrl.java class to delete the file from the S3 bucket and return the response back to the user. This method reads the
filename query parameter coming in the
4. Run the Application
To execute the application, compile the project and right-click on the
Run As -> Java Application.
5. Project Demo
Once the application is started successfully, open the Postman tool. Hit the following URL (
HTTP DELETE request) to delete the file from the S3 bucket.
Once the file is deleted successfully from the S3 bucket, the API will return the
200 OK response as shown in Fig. 3.
And the users can navigate to the AWS console to validate that the same file is now deleted in the S3 bucket.
That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!
In the above sections, developers learned how to delete a file from an S3 bucket via a spring boot application. Developers can download the sample application from the Download section.
This was an example of deleting a file from the AWS S3 bucket via a spring boot application.
You can download the full source code of this example here: Spring boot and AWS S3: Delete file