Galera, I already researched in several forums and no solution solved my problem. When I upload file to S3 using the Amazon API. The following exception is thrown.
com.amazonaws.SdkClientException: Unable to calculate MD5 hash: teste.txt (O sistema não pode encontrar o arquivo especificado)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1622)
Caused by: java.io.FileNotFoundException: teste.txt (O sistema não pode encontrar o arquivo especificado)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
Here are my classes involved:
S3Config.java
@Bean
public AmazonS3 s3client() {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsId, awsKey);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(Regions.fromName(region))
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.disableChunkedEncoding()
.build();
return s3Client;
}
S3Service.java
@Override
public String uploadFile(Conteudo conteudo, MultipartFile multipartFile) {
String keyname = "teste";
try {
File file = conveteArquivo(multipartFile);
s3.putObject(new PutObjectRequest(bucketName, keyname, file));
logger.info("======================= Upload File - Done! ============================");
} catch (AmazonServiceException e) {
logger.info("Exceção da AmazonSeviceException em requisições PUT, devido:");
logger.info("Mensagem de erro: " + e.getMessage());
logger.info("Código HTTP: " + e.getStatusCode());
logger.info("Código de erro da AWS: " + e.getErrorCode());
logger.info("Tipo do erro: " + e.getErrorType());
logger.info("ID da requisição: " + e.getRequestId());
} catch (AmazonClientException e) {
logger.info("Exceção da AmazonClientException");
logger.info("Mensagem de erro: " + e.getMessage());
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return linkBucket + keyname;
}
private File conveteArquivo(MultipartFile multipart) throws IllegalStateException, IOException {
File convFile = new File( multipart.getOriginalFilename());
multipart.transferTo(convFile);
return convFile;
}
Controller
@PostMapping
public ResponseEntity<Conteudo> publicaConteudo(@RequestPart("file") MultipartFile file, @RequestPart String conteudo ) throws JsonParseException, JsonMappingException, IOException {
Conteudo conteudoConvertido = new ObjectMapper().readValue(conteudo, Conteudo.class);
Conteudo conteudoArmazenado = conteudoService.uploadConteudo(conteudoConvertido, file);
return ResponseEntity.ok(conteudoArmazenado);
}
To using version 1.11.106 of the Amazon SDK. Does anyone have any idea what the reason for this exception might be? I do not know what to do.