Upload Operation in AWS S3 Bucket using SpringBoot REST API
Tags: aws, springboot, s3buck, rest
Published: 2023-02-02 14:19:30

## Table of Contents

**Steps Follows on AWS**
- Account Creation
- Amazon S3 Bucket Creation
- Access Key Creation

**Steps Follows on SpringBoot**
- SpringBoot Project Creation
- Add AWS Dependency
- Metthod Creation
- Check the API



**First Step Create Account in AWS**

**1.) Go to AWS Free tier section**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3wrs6u0ycnyb1m5dqyx.png)

**2.) Account created Sucessfully Message**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ni088e0m4f4k5rbknj1w.png)

**3.) Go the AWS Management console section**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6znlv339dja74tcns257.png)

4.) Search S3 bucket on aws management panel


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uu9fjotysa24aqjltfvl.png)

5.) Create bucket


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3e05anqqpc1wcigdtfpn.png)
6.) write unique name of the bucket and then click to create

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66bbiepipjtq0cbszhxz.png)

7.) After created the bucket go to the security credentials

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56epjkk7tlr96wd7yuns.png)

8.) Create Access Key


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28mtnz0prv8vmjwhkm55.png)

9.) Copy Access key And Screet in Notepad

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bvr0y5wsxmwepzzgp5kh.png)

**Second Step for the SpringBoot**

1.) Generate SpringBoot project using Spring Initializer

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nc3f6h5j5bg2r8j5a1vs.png)

2.) Extract the zip file and Open the project in IntelijIDEA

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chl5wjabz6fz2tptp3xw.png)

3.) Create Controller and Service package inside the main package and create the BucketController and BucketService class

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ne9t8h7d64orr1b2291d.png)

4.) Write some details on application.properties file

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nm7w3v600295ywpy1cse.png)

5.) import the aws core dependency
`<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.486</version>
</dependency>`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u429c8ow2twowmv77g83.png)

6.) Create the AWS Config class inside the Configuration package

```

package com.shashank.bucketProject.configuration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
    @Configuration
    public class AWSConfig {
        @Value("${access-key}")
        private String accessKey;

        @Value("${secret-key}")
        private String accessSecret;
        @Value("${region}")
        private String region;

        @Bean
        public AmazonS3 s3Client() {
            AWSCredentials credentials = new BasicAWSCredentials(accessKey, accessSecret);
            System.out.println("Hello AWS");
            return AmazonS3ClientBuilder.standard()
                    .withCredentials(new AWSStaticCredentialsProvider(credentials))
                    .withRegion(region).build();
        }
    }
```



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3z0g669vrklar6uyv687.png)

7.) write some code in BucketService class

```
package com.shashank.bucketProject.service;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.PutObjectRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 @Service
    public class BucketService {
        @Value("${bucket-name}")
        private String bucketName;
        @Autowired
        private AmazonS3 amazonS3;

        public String uploadFile(MultipartFile file) {
            File fileObj = convertMultiPartFileToFile(file);
            String fileName =  file.getOriginalFilename();
            amazonS3.putObject(new PutObjectRequest(bucketName, fileName, fileObj));
            fileObj.delete();
            return "File uploaded : " + fileName;
        }
        private File convertMultiPartFileToFile(MultipartFile file) {
            File convertedFile = new File(file.getOriginalFilename());
            try (FileOutputStream fos = new FileOutputStream(convertedFile)) {
                fos.write(file.getBytes());
            } catch (IOException e) {
                System.out.println("Error converting multipartFile to file" + e);
            }
            return convertedFile;
        }
}



```



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ytfhogotbome04ftase.png)


8.) write some code in BucketController class

```
package com.shashank.bucketProject.controller;

import com.shashank.bucketProject.service.BucketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api")
public class BucketController {

    @Autowired
    private BucketService bucketService;

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam(value = "file") MultipartFile file) {
        return new ResponseEntity<>(bucketService.uploadFile(file), HttpStatus.OK);
    }
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/goimd2zy4wclk8zm1p43.png)

9.) Before Running the project change the version of spring application 2.7.8

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yn396y408rbywsxoa5ai.png)

10.) Run the springboot Application

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3uzxqwb9l03y8euswviy.png)

11.) Open the Postman and check the api is working or not

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9n3ltnf5xpr7jamsy6w.png)

12.) Now check on the aws file is uploaded or not

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o0bjyptcfsgnh3dck8tc.png)