public void upload(MultipartFile multipartFile, boolean publicAccess) {
String fileName = this.generateUniqueFilename(multipartFile);
try {
File file = new File(Objects.requireNonNull(fileName));
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(multipartFile.getBytes());
outputStream.close();
PutObjectRequest request = new PutObjectRequest(this.awsS3UploadBucket, fileName, file);
if (publicAccess) {
request.withCannedAcl(CannedAccessControlList.PublicRead);
}
this.amazonS3.putObject(request);
} catch (Exception exception) {
logger.error("error [" + exception.getMessage() + "] occurred while uploading [" + fileName + "] ");
}
}
The generateUniqueFilename
generates a unique name for the uploaded file. For example 143119066552807424.png
.
When the code runs I got this exception
error [The request signature we calculated does not match the signature you provided. Check your key and signing method.
(Service: Amazon S3; Status Code: 403; Error Code: SignatureDoesNotMatch; Request ID: XXX; S3 Extended Request ID: XXXX)] occurred while uploading [143119066552807424.png]
I did some research on google but found no resource which explains how I can sign the request right. Can anyone explain why this code isn't running?