Upload file to S3 on EC2 instance - permission denied

So, in my application, users will be able to upload their photos to the S3 repository, after which I will save the returned URL in the database.

The problem I am facing is that when I deploy the application in my Elastic Beanstalk environment, I can no longer store photos in S3 storage due to this error that I found in the catalina.out log:

java.io.IOException: Permission denied at java.io.UnixFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(File.java:1012) at com.common.util.file.FileUtil.convert(FileUtil.java:17) 

How can I set the resolution to fix this error?

[EDIT] When I run the application on the local host, I can upload the file to the S3 bucket. I used this sample code in this link as an implementation template for uploading a file.

Here is the code I use to convert a multi-page file to a file:

 public File convert(MultipartFile file) throws IOException { File convFile = new File(file.getOriginalFilename()); convFile.createNewFile(); // Cause IOException FileOutputStream fos = new FileOutputStream(convFile); fos.write(file.getBytes()); fos.close(); return convFile; } 
+2
java amazon-s3 amazon-web-services amazon-ec2
source share
2 answers

Do not add aws credentials in your application. Create an IAM role that must have permission for AWS S3. When starting the application using Elastic Beanstalk, attach this IAM role to the EC2 instance.

Your application uses the IAM role to authenticate AWS S3 to download images. It is recommended that you use an IAM role with a specific permission to run an EC2 instance through the AutoScaling or Elastic Beanstalk group or directly from the EC2 control panel.

+2
source share

The link to the sample code that you specified uses ProfileCredentialsProvider , which passes AWSCredentials from the profile configuration file for the default profile ( more ).

You must either (1) copy the configuration file from your local machine to the EC2 instance, or (2) use a different method to provide AWS credentials in the SDK. See Working with AWS credentials , for example.

0
source share

All Articles