0

I am trying to upload a file to S3 Bucket using Spring Boot and spring-cloud-starter-aws.

It works fine in my local, but on being uploaded to EBS, the files are not getting uploaded.

The code:

public String uploadFileToS3Bucket(MultipartFile file, String path, boolean enablePublicReadAccess) {
        File fileObj = convertMultiPartFileToFile(file);
        String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
        fileName = path +fileName;
        try {
            s3Client.putObject(new PutObjectRequest(bucketName, fileName, fileObj));
            fileObj.delete();
        }catch (Exception e) {
            System.out.println("File Upload Failed due to the following: "+ e);
        }
        
        System.out.println("File uploaded : " + fileName);
        return fileName;
    }

convertMultiPartFileToFile method:

     private File convertMultiPartFileToFile(MultipartFile file) {
        Random rnd = new Random();
        String randomVal = String.valueOf(100000 + rnd.nextInt(900000));
        String fileName = randomVal + "-" + file.getOriginalFilename();

        System.out.println("upload file name test:::: " + fileName);

        String filePath = "";

        logger.info(env.getProperty("testing"));
        File convertedFile = null;

        try {
            
            convertedFile = new File(filePath + fileName);
            
            System.out.println("The converted file: "+ convertedFile);
            convertedFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(convertedFile);
            fos.write(file.getBytes());
            fos.close();

        } catch (Exception e) {
            System.out.println("Error converting multipartFile to file:" + e);
        }

        return convertedFile;
    }

In the log file I get the below error:

Error converting multipartFile to file:java.io.IOException: Permission denied
File Upload Failed due to the following: com.amazonaws.SdkClientException: Unable to 
calculate MD5 hash: 746350-about_us_work.jpeg (No such file or directory)

The EBS IAM Role has S3FullAccess.

It works perfectly from the local, but not so in the server. Any ideas what could be the issue?

Anamik Adhikary
  • 401
  • 1
  • 8
  • 27

2 Answers2

0

Solved it using reference from Unable to calculate MD5 : AWS S3 bucket

Updated Code looks like this:

public String uploadFileToS3Bucket(MultipartFile file, String path, boolean enablePublicReadAccess) {
        File fileObj = convertMultiPartFileToFile(file);
        String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
        fileName = path +fileName;
        try {
            InputStream is=file.getInputStream();        
            s3Client.putObject(new PutObjectRequest(bucketName, fileName, is, new ObjectMetadata()));
            fileObj.delete();
        }catch (Exception e) {
            System.out.println("File Upload Failed due to the following: "+ e);
        }
        
        System.out.println("File uploaded : " + fileName);
        return fileName;
    }
Anamik Adhikary
  • 401
  • 1
  • 8
  • 27
0

I don't understand the need for fileObj.delete(). I tried to upload a file with the following code and works:

 public void uploadFileToS3Bucket(MultipartFile file) throws IOException {
    s3client.putObject(
        new PutObjectRequest(
            bucketName, file.getOriginalFilename(), file.getInputStream(), new ObjectMetadata()));
  }
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Jeremy Caney Jun 07 '22 at 00:30
  • 1
    Sorry my natural language is not english but I will be more carefully in my next answer – Lucas Otávio Pereira Rezende Jun 08 '22 at 01:13
  • 1
    Your English is totally fine. I just think this answer would be more valuable to future readers if you edited it to include more of an explanation of what you're doing, and why it solves the problem. (For what it's worth, the OP's own answer would also benefit from this.) – Jeremy Caney Jun 08 '22 at 01:16