aws-sdk-java-v2
aws-sdk-java-v2 copied to clipboard
Signing Get Requests with AWS4Signer: The request signature we calculated does not match the signature you provided
I am trying to generate a signed URL to fetch a file stored in my AWS S3 Bucket. The Link generated is to expire after an hour.
The accessKey and SecretKey have been checked and are able to upload files in the relevant module.
Expected Behavior
The Link Generated should download the file
Current Behavior
The Link generated produced the following output on browser
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your key and signing method.
</Message>
<AWSAccessKeyId><Access Key></AWSAccessKeyId>
<StringToSign>
AWS4-HMAC-SHA256 20180828T031727Z 20180828/ap-south-1/s3/aws4_request ec1203b0c718bc306b351e17e6014965860a9af6743a559f986af8fbbee0845d
</StringToSign>
<SignatureProvided>
6e6abcdd52db1746723446f41566f1102072b42a8230a08fe7f1d555e98c931a
</SignatureProvided>
<StringToSignBytes>
41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 0a 32 30 31 38 30 38 32 38 54 30 33 31 37 32 37 5a 0a 32 30 31 38 30 38 32 38 2f 61 70 2d 73 6f 75 74 68 2d 31 2f 73 33 2f 61 77 73 34 5f 72 65 71 75 65 73 74 0a 65 63 31 32 30 33 62 30 63 37 31 38 62 63 33 30 36 62 33 35 31 65 31 37 65 36 30 31 34 39 36 35 38 36 30 61 39 61 66 36 37 34 33 61 35 35 39 66 39 38 36 61 66 38 66 62 62 65 65 30 38 34 35 64
</StringToSignBytes>
<CanonicalRequest>
GET /<bucket name>/imageTestLogo.svg X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=<access key>%2F20180828%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Date=20180828T031727Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host host:s3.ap-south-1.amazonaws.com host UNSIGNED-PAYLOAD
</CanonicalRequest>
<CanonicalRequestBytes>
47 45 54 0a 2f 73 74 67 70 6f 63 6b 65 74 70 69 6c 6c 73 69 6d 61 67 65 73 2f 69 6d 61 67 65 54 65 73 74 4c 6f 67 6f 2e 73 76 67 0a 58 2d 41 6d 7a 2d 41 6c 67 6f 72 69 74 68 6d 3d 41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 26 58 2d 41 6d 7a 2d 43 72 65 64 65 6e 74 69 61 6c 3d 41 4b 49 41 4a 49 41 36 4f 58 4a 36 5a 50 58 34 45 5a 55 51 25 32 46 32 30 31 38 30 38 32 38 25 32 46 61 70 2d 73 6f 75 74 68 2d 31 25 32 46 73 33 25 32 46 61 77 73 34 5f 72 65 71 75 65 73 74 26 58 2d 41 6d 7a 2d 44 61 74 65 3d 32 30 31 38 30 38 32 38 54 30 33 31 37 32 37 5a 26 58 2d 41 6d 7a 2d 45 78 70 69 72 65 73 3d 33 36 30 30 26 58 2d 41 6d 7a 2d 53 69 67 6e 65 64 48 65 61 64 65 72 73 3d 68 6f 73 74 0a 68 6f 73 74 3a 73 33 2e 61 70 2d 73 6f 75 74 68 2d 31 2e 61 6d 61 7a 6f 6e 61 77 73 2e 63 6f 6d 0a 0a 68 6f 73 74 0a 55 4e 53 49 47 4e 45 44 2d 50 41 59 4c 4f 41 44
</CanonicalRequestBytes>
<RequestId>5FC3A1A9AB1A9231</RequestId>
<HostId>
/w3XcTplWGMQ7VwMQ6C7rIFVs0/kCEX2SD5/0j1MJNBLmByapAns3F3Bz0H0lyzypyrP18tNBlM=
</HostId>
</Error>
Possible Solution
Steps to Reproduce (for bugs)
@Override
public CompletableFuture<String> getSignedUrl(String key) {
Aws4PresignerParams params = Aws4PresignerParams.builder()
.awsCredentials(this.credentials)
.signingName("s3")
.signingRegion(Region.AP_SOUTH_1)
.expirationTime(Instant.ofEpochSecond(60*60))
.build();
SdkHttpFullRequest request = SdkHttpFullRequest.builder()
.host("s3.ap-south-1.amazonaws.com")
.encodedPath("<bucket name>/" + key)
.method(SdkHttpMethod.GET)
.protocol("https")
.build();
SdkHttpFullRequest result = Aws4Signer.create().presign(request,params);
return CompletableFuture.completedFuture(result.getUri().toString());
}
Context
I want to generate an Expirable download link.
Your Environment
- AWS Java SDK version used: Amazon V2 SDK for Java - Developer Preview 11
- JDK version used: Java 8 update 101
- Operating System and version: Windows 10
The New API uses a different Signer Class for Amazon S3 Service : AwsS3V4Signer
@Override
public CompletableFuture<String> getSignedUrl(String key) {
System.out.println(this.credentials);
Aws4PresignerParams params = Aws4PresignerParams.builder()
.expirationTime(Instant.ofEpochSecond(60*60))
.awsCredentials(this.credentials)
.signingName("s3")
.signingRegion(Region.AP_SOUTH_1)
.build();
SdkHttpFullRequest request = SdkHttpFullRequest.builder()
.encodedPath(("/stgpocketpillsimages/" + key))
.host("s3.ap-south-1.amazonaws.com")
.method(SdkHttpMethod.GET)
.protocol("https")
.build();
SdkHttpFullRequest result = AwsS3V4Signer.create().presign(request,params);
System.out.println(Json.toJson(result));
return CompletableFuture.completedFuture(result.getUri().toString());
}