Java Amazon SDK presign url bug or incorrect implementation?

By : Chiquis
Source: Stackoverflow.com
Question!

I am just wondering if this is a bug or im incorrectly using the sdk.

The Presigned URL generation code is as follows:

java.util.Date expiration = new java.util.Date();
long milliSeconds = expiration.getTime();
milliSeconds += 1000 * 60 * 60; // Add 1 hour.
expiration.setTime(milliSeconds);

URL url = s3.generatePresignedUrl(userPicturesBucket, "walala", expiration, HttpMethod.PUT);

But this will generate a presigned ulr in where the "Signature" is wrong.

Signature=7iw7DfZ3Ed24kQpsV5Hp9VrJ%2BMo%3D

I can test this by using RESTClient to send a PUT request on the complete address. I will get a forbidden response saying that SignatureDoesNotMatch.

However If i use the Amazon S3 Signature Tester to sign the same request I will get a different "Signature"

Signature=ztp2nD6gVurYhAvQojpZhReSuwE%3D

This one however DOES work, and sending the PUT request will return a 200 response and the object will be created.

My question is, Is this an amazon sdk for java bug? Or why is the signature different.

Thanks

By : Chiquis


Answers

There is a possible error in your code:

generatePresignedUrlRequest.setExpiration(expiration);
// Next line should use the 'generatePresignedUrlRequest' object
URL url = s3.generatePresignedUrl(generatePresignedUrlRequest);


I am not 100% sure but i believe the problem was related to the fact that the signing in the url also has to include the object type and format when sending the request and the program i was using to check would automatically add charset field even if it was an image.

So adding this "PUT\n\nimage/jpeg; charset=UTF-8\n" to the URL to sign seems to have fixed the problem.

By : Chiquis


This video can help you solving your question :)
By: admin