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?