Introduction
In this article we will learn how to upload a file to Amazon S3 Bucket using Spring boot. As you know most mobile or web applications have features for uploading files such as video images, etc. So as a programmer you should choose the best way to store and loosen it. Store these files. There are different ways to deal with:
- Save directly on the server where the application was deployed
- Stored in database with binary file type
- Used in cloud storages (online storage places)
In fact, most developers use a third approach using S3 Bucket Service of Amazon . And here is the reason why it is so popular:
- Easy to write code to upload any file
- Amazon supports most programming languages
- You can view all uploaded files directly using the web interface
- You can manipulate manually such as upload / delete files using the web interface
Step 1 – Create Amazon S3 bucket.
To start using S3 Bucket you must create an account on the Amazon website .
Once you have an account, the next step is to create the S3 Bucket where the uploaded files are stored. On the menu bar select Services -> S3
Then press the Create bucket button
Enter a bucket name (The bucket name is unique) and select the area nearest you. Click the Create button
Note: Amazon will give you 5GB for free each year for you to store. If you use more than 5GB per year you will have to pay
Your buket has now been created, but you will need to provide user access to this bucket. It is not secure when you give your root user access key to others so you need to create a new IAM use this user who only has permission to use S3 Bucket
1 2 | Định danh AWS và Quản lý quyền truy cập (IAM) là một dịch vụ web giúp bạn kiểm soát truy cập an toàn vào các tài nguyên AWS |
Now create the user. Select Services -> IAM . On the left navigation pane select User then select Add user .
Enter the user name and check the box for Accesss type ‘Programmatic access’ . Click the next button.
You must add permissions for the user just created. Click ‘ Attach existing policy directly ‘, in the search box enter ‘s3’ in all searched results select AmazonS3FullAccess
Then click next and Create User you will see the Access key ID and Secret access key will be used in the next section. Now the configuration of S3 Bucket is complete. Let’s go to the next section to write the code to upload the file to the newly created buket s3
Step 2 – Write the Code upload file to S3 Buket.
Create Spring Boot project and add amazon dependency to pom.xml file
1 2 3 4 5 6 | <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk</artifactId> <version>1.11.133</version> </dependency> |
Then add the s3 bucket properties you just created above to the application.yml file
1 2 3 4 5 6 | amazonProperties: endpointUrl: https://s3.us-east-2.amazonaws.com accessKey: XXXXXXXXXXXXXXXXX secretKey: XXXXXXXXXXXXXXXXXXXXXXXXXX bucketName: your-bucket-name |
Create class AmazonClient
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @Service public class AmazonClient { private AmazonS3 s3client; @Value("${amazonProperties.endpointUrl}") private String endpointUrl; @Value("${amazonProperties.bucketName}") private String bucketName; @Value("${amazonProperties.accessKey}") private String accessKey; @Value("${amazonProperties.secretKey}") private String secretKey; @PostConstruct private void initializeAmazon() { AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey); this.s3client = new AmazonS3Client(credentials); } } |
AmazonS3 is a class from the amazon library. All other fields are AWS S3 bucket information declared in application.yml. Anotation @ Value will read the values from application.yml file
initializeAmazon () is a method to set amazon login information for our application. Anotation @ PostConstruct to run this method after the constructor will be called because the fields marked with @ Value have null values in the constructor.
The method of uploading files to the S3 bucket requires a File type parameter, but our data transfer will be of the MultipartFile type. We will need to add a method to convert MultipartFile -> file
1 2 3 4 5 6 7 8 | private File convertMultiPartToFile(MultipartFile file) throws IOException { File convFile = new File(file.getOriginalFilename()); FileOutputStream fos = new FileOutputStream(convFile); fos.write(file.getBytes()); fos.close(); return convFile; } |
Also you can upload a file multiple times so we create a unique name for each upload. Use timestamp to mark and replace filename spaces with underscores to avoid future problems.
1 2 3 4 | private String generateFileName(MultipartFile multiPart) { return new Date().getTime() + "-" + multiPart.getOriginalFilename().replace(" ", "_"); } |
Next add the uploadFileTos3bucket method that uploads the file to the S3 bucket
1 2 3 4 5 | private void uploadFileTos3bucket(String fileName, File file) { s3client.putObject(new PutObjectRequest(bucketName, fileName, file) .withCannedAcl(CannedAccessControlList.PublicRead)); } |
In this method we have added PublicRead permission. It means that anyone who has the path of the file can access the file. This attribute is usually only applied to image files
Finally, we’ll combine the methods just created above to create a service that will be called from the controller. This method will save the file to the s3 bucket and return the path of the newly uploaded file that you can store in the database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public String uploadFile(MultipartFile multipartFile) { String fileUrl = ""; try { File file = convertMultiPartToFile(multipartFile); String fileName = generateFileName(multipartFile); fileUrl = endpointUrl + "/" + bucketName + "/" + fileName; uploadFileTos3bucket(fileName, file); file.delete(); } catch (Exception e) { e.printStackTrace(); } return fileUrl; } |
Next we create BucketController.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @RestController @RequestMapping("/api/storage/") public class BucketController { private AmazonClient amazonClient; @Autowired BucketController(AmazonClient amazonClient) { this.amazonClient = amazonClient; } @PostMapping("/uploadFile") public String uploadFile(@RequestPart(value = "file") MultipartFile file) { return this.amazonClient.uploadFile(file); } } |
The uploadFile method takes a MultipartFile parameter. Now that we have finished the code, let’s go to the test. Create a request using Postman, select the POST method in BODY, select the form-data, enter a key file and select the value of the File value type and select any file from your PC. friend
You visit your s3 bucket and you will see the uploaded image
The article comes here is the end. Hope this article was helpful for you. If you have any questions please leave a comment below this article.
Thank you for reading this article References at https://medium.com/oril/uploading-files-to-aws-s3-bucket-using-spring-boot-483fcb6f8646