0

enter image description here

Above image shows the structure of my application deployment.. Spring boot application have the below aws configuration. enter image description here

The application is working on local. I can able to write file to s3 bucket. when i deploy the code on different server as i mentioned above image .Its failed to write s3 bucket Showing below error

Error while uploading the file to S3java.io.FileNotFoundException: sample.txt (Permission denied)"

Note: These servers are in different location.

How can i overcome this issue. I have a solution

  1. Write upload file to UI Server folder and using a script to upload in the s3 Anyone have other solution please suggest.

Edit 1: 1.I can able to receive request form UI server.but file is not present in the request 2.other get request are working Edit 2

Controller

 @PostMapping("/request")
    ReturnStatus saveRequest(HttpServletRequest request,
                             
                             @RequestParam(value = "file", required = true)
                                     MultipartFile uploadedFile) throws InvalidInputException {
        AuthDto auth = (AuthDto) request.getAttribute(AUTH_DTO);
        return requestService.saveRequest(
                new RequestParams( uploadedFile));
    }

Service Class Logic

File uploadFile = convertMultiPartToFile(params.getLookUpValues());
            s3ClientBuilder.build().putObject(new PutObjectRequest(s3BucketName, fileName, uploadFile));

public static 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;
    }   

Edit 3

enter image description here

create a temp folder inside the UI server tomcat

Once i click on upload file saved to temp and request goes to elastic beanstalk and save the meta data.

How the s3 bucket file upload work?
Need to write any separate script? if yes how its trigger

Pease correct me if am wrong

arj
  • 887
  • 1
  • 15
  • 37
  • Could you pls check if you are receiving the file correctly in your spring boot correctly. From the exception it seems like it is not s3 issue but file itself is not getting resolved. – Sukhmeet Sethi Dec 03 '21 at 08:51
  • that is correct. otherwise i will not work in my local – arj Dec 03 '21 at 09:29
  • Please post your spring-boot code. Are you saving the file locally first and then try to upload it? If this is the case, in EB you need to properly configure access to local filesystem. – kgiannakakis Dec 03 '21 at 10:22
  • @kgiannakakis added. – arj Dec 03 '21 at 10:33

1 Answers1

1

There are some spring-boot properties for directories used that default to /tmp. Your EB user won't have access to this folder. These settings have allowed us to use multipart upload with Spring Boot in an Elastic Beanstalk environment:

server.tomcat.basedir: ${user.dir}
spring.http.multipart.location: ${user.dir}\

The error is probably coming from this line:

File convFile = new File(file.getOriginalFilename());

You are trying to save a file in base dir, but you don't have access to it. Follow above advice. Alternatively, read this question on how to properly create a tmp folder.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • server.tomcat.basedir-> this will be the UI Server tomcat location right? – arj Dec 03 '21 at 10:55
  • Yes for embedded tomcat. See https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html and https://docs.spring.io/spring-boot/docs/1.0.1.RELEASE/reference/html/common-application-properties.html. You need to set the multipart location to a folder you have access to. – kgiannakakis Dec 03 '21 at 11:01
  • @ kgiannakakis can u please explain the flow of an upload request – arj Dec 03 '21 at 16:28
  • Please also post the code of your convertMultiPartToFile method. You are probably creating a file in a temp location. Locally you have access, but not in EB. – kgiannakakis Dec 03 '21 at 17:00
  • @ kgiannakakis added – arj Dec 03 '21 at 17:05
  • Read my updated answer. Your application runs with a Linux user that is restricted from accessing most of the local file system. You need to find a location that your user is permitted to write files. – kgiannakakis Dec 03 '21 at 17:16
  • @ kgiannakakis do we need to write any script for uploading file? it will take care of my current upload logic? – arj Dec 06 '21 at 11:21
  • No. Read these (somehow) related questions to understand what the problem is: https://stackoverflow.com/questions/34630698/aws-ec2-tomcat-permission-denied-creating-writing-to-file, https://stackoverflow.com/questions/63275250/spring-boot-on-aws-elastic-beanstalk-and-logging-to-a-file – kgiannakakis Dec 06 '21 at 11:46