Unable to calculate MD5 hash in upload file using AWS Sdk in java

Asked

Viewed 196 times

2

Guys, I’ve searched several forums and no solution solved my problem. When I upload the file to S3 using the Amazon API. The following exception shall be made.

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)

Follow 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);
}

I’m using version 1.11.106 of the Amazon SDK. Does anyone have any idea what might be the reason for this exception ? I don’t know what to do.

  • The exception seems to me that the local file is not being found, it is probably being searched in a different directory than you expect, try to use an absolute path or check where jvm is being executed. Also, avoid passing credentials directly

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.