Search code examples
httpurlgetrequestspring-resttemplate

SignatureDoesNotMatch error using restTemplate to get url request of S3


I'm trying to send a HTTP GET request of S3 using restTemplate and I get SignatureDoesNotMatch error: The request signature we calculated does not match the signature you provided. Check your key and signing method.

Dose anyone know what can cause this error?


Solution

  • Found the problem!

    Apparently that my question wasn't accurate, just like the error I got from S3 - not indicative.

    What I was actually searching for is how to download a file from S3 url using Rest Template. So I had to add the following configuration and used this code to solve it:

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
    HttpHeaders headers = new HttpHeaders();
    String url = URLDecoder.decode(path, "UTF-8");
    ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headers), byte[].class);