arj
arj

Reputation: 983

AWS S3 uploading file does not work when deployed UI and middle tire on different network servers but works in localhost SpringBoot

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

Upvotes: 0

Views: 956

Answers (1)

kgiannakakis
kgiannakakis

Reputation: 104178

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.

Upvotes: 1

Related Questions