Configuring AWS S3 bucket in Spring boot project for file uploading
Posted By : Amit Maurya | 28-Apr-2022
Amazon's S3 (Simple Storage Service) is a very reliable storage service for uploading and retrieving files. It is very easy to integrate. In this tutorial we will learn how we can integrate AWS S3 bucket into a spring boot project for files uploading.
1. Maven Dependency
First of all we need to add maven dependency for amazon java sdk.
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk</artifactId> <version>1.11.880</version> </dependency>
Note: Check in mvn central repository for latest version.
2. Credentials
Create an AWS account and get the following credentials.
a.) ENDPOINT_URL
b.) BUCKET_NAME
c.) ACCESS_KEY
d.) SECRET_KEY
3. Create a Controller
Create a Rest Controller to get MultipartFile from the client to upload on the S3 bucket.
4. Intialize amazon Client
Create a Post Construct method to initialize the Amazon client.
@PostConstruct private void initializeAmazon() { AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY); this.s3client = AmazonS3ClientBuilder.standard().withRegion(Regions.AP_SOUTHEAST_1).withCredentials(new AWSStaticCredentialsProvider(credentials)).build(); }
5. MultipartFile to file
Create a method to convert multipart file to file. So, it can be uploaded on the S3 bucket.
private File convertMultiPartToFile(MultipartFile file) throws IOException { File convFile = new File(System.getProperty("java.io.tmpdir") + File.separator +Objects.requireNonNull(file.getOriginalFilename())); try { file.transferTo(convFile); LOGGER.info("File created successfully"); } catch (IOException e) { LOGGER.error("Exception in file convert service"); } return convFile; }
6. Method to Generate file name
This method will generate a file name for the file by which it can be identified on the S3 bucket.
private String generateFileName(MultipartFile multiPart) { return new Date().getTime() + "-" + Objects.requireNonNull(multiPart.getOriginalFilename()).replace(" ", "_"); }
7. Method to upload File
This method uploads the file on the S3 bucket.
private void uploadFileTos3bucket(String fileName, File file) { s3client.putObject( new PutObjectRequest(BUCKET_NAME, fileName, file).withCannedAcl(CannedAccessControlList.PublicRead)); }
8. Create a service to upload Multipart files on S3
This method takes a MultipartFile as input and uploads that file to the server and return a URL by which we can access that particular file from the browser.
public String uploadFile(MultipartFile multipartFile) { String fileUrl = ""; try { LOGGER.info("In amazon file upload"); File file = convertMultiPartToFile(multipartFile); String fileName = generateFileName(multipartFile); fileUrl = ENDPOINT_URL + "/" + fileName; uploadFileTos3bucket(fileName, file); Utils.delete(file); return fileUrl; } catch (Exception e) { LOGGER.error("Error file uploading file", e); } return null; }
9. Method to delete the file from the S3 bucket
This method deletes the file from the S3 bucket.
public void deleteFileFromS3Bucket(String fileUrl) { String fileName = fileUrl.substring(fileUrl.lastIndexOf('/') + 1); s3client.deleteObject(new DeleteObjectRequest(BUCKET_NAME, fileName)); }
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Amit Maurya
Amit Maurya is a highly skilled Backend Developer with more than 2 years of experience in developing RESTAPIs and Microservices. He has expertise in using Spring Boot framework, Hibernate, Java 8, and JavaEE, and he has worked with various databases such as MySQL, PostgreSQL, Oracle, Redis, and more.He has also worked on implementing payment gateways such as Stripe, Paypal, Cryptocurrency (Metamask), Android In-App Purchases, and Apple in App Purchases. Furthermore, he has experience in implementing and maintaining Streaming servers like Ant Media and Agora. He is proficient in using version control systems like GIT and source code management tools such as GitHub and GitLab, including command line applications.He has worked on several projects, including TutorX, Fabtrack, Toosi WhatsApp Chatbot Integration, and Virtuosica.