|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
Using Jets3t to generate encoded Policy DocI am little bit confused on where to put this question, so putting it on both Amazon S3 forum and Jets3T
I followed up thread of James , JBoboli , Sarathy on this on Amazons S3 Forum with title : Browser Uploads to S3 using HTML POST Forms but still could not get this working. Here is my Java/Groovy Code def getPolicy64Code(String bucketName) { Calendar cal = Calendar.instance; cal.add(Calendar.HOUR, 24); Date expiration = cal.getTime(); String dateStr = ServiceUtils.formatIso8601Date(expiration) dateStr = "2009-02-01T12:00:00.000Z" //fixed for now String key = "mikey" def buckCond = S3Service.generatePostPolicyCondition_Equality("bucket", "harvesttrial") def keyCond = S3Service.generatePostPolicyCondition("starts-with",'key', key) def aclCond = S3Service.generatePostPolicyCondition_Equality("acl", "private") def contCond = S3Service.generatePostPolicyCondition("starts-with",'Content-Type', "") def sizeCond = S3Service.generatePostPolicyCondition_Range(0, 1048576) String[] conditions = [buckCond, keyCond, aclCond,contCond,sizeCond] String condStr = ServiceUtils.join(conditions,",") String policyDocument = "{\"expiration\": \"" + dateStr + "\", \"conditions\": ["+condStr + "]}" log.debug(policyDocument) String policyB64 = (new BASE64Encoder()).encode( policyDocument.getBytes("UTF-8")).replaceAll("\n","").replaceAll("\r",""); //String policyB64 = //ServiceUtils.toBase64(policyDocument.getBytes(Constants.DEFAULT_ENCODING)); log.debug("Encrypted PolicyDoc is $policyB64") return policyB64 } This generates the following policy String {"expiration": "2009-02-01T12:00:00.000Z", "conditions": [ {"bucket": "harvesttrial"}, ["starts-with", "$key", "mikey"], {"acl": "private"}, ["starts-with", "$Content-Type", ""], ["content-length-range", 0, 1048576]] } And Here is my Corresponding HTML Page <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Upload File</title> </head> <body> <h1>Sample line</h1> <form action=" http://s3.amazonaws.com/harvesttrial" method="post" enctype="multipart/form-data"> <pre>< input type = "text" id = "key" name = "key" value = "mikey.pdf" / > < input type = "hidden" name = "AWSAccessKeyId" value = 14M0Q3PZ3W9HHZZN6AR2 / > < input type = "hidden" name = "acl" value = "private" > < input type = "hidden" name = "policy" value = eyJleHBpcmF0aW9uIjogIjIwMDktMDItMDFUMTI6MDA6MDAuMDAwWiIsICJjb25kaXRpb25zIjogW3siYnVja2V0IjogImhhcnZlc3R0cmlhbCJ9LFsic3RhcnRzLXdpdGgiLCAiJGtleSIsICJtaWtleSJdLHsiYWNsIjogInByaXZhdGUifSxbInN0YXJ0cy13aXRoIiwgIiRDb250ZW50LVR5cGUiLCAiIl0sWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsIDAsIDEwNDg1NzZdXX0=/ > < input type = "hidden" name = "signature" value = Co/JIwww+8ggVTVNGM1CVeoN1mM= / > < input type = "text" name = "content-type" value = "application/pdf" / > File to upload to S3: </pre> <input name="file" type="file"> <br> <input type="submit" value="Upload File to S3"> </br> </form> </body> The encoded Policy generated using the ServiceUtils.Base64 or encode method as used above, is different fromthe one I generate from http://s3.amazonaws.com/doc/s3-example-code/post/post_sample.html using the exact same Policy String (Thats the reason I hardcoded the dateString) And the policy code this Post Sample Link generates works fine. I am very confused on how to proceed Here is what the website PostSample Generates <input type="hidden" name="policy" value="ewogICJleHBpcmF0aW9uIjogIjIwMDktMDItMDFUMTI6MDA6MDAuMDAwWiIsCiAgImNvbmRpdGlvbnMiOiBbCiAgICB7ImJ1Y2tldCI6ICJoYXJ2ZXN0dHJpYWwiIH0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAibWlrZXkiXSwKICAgIHsiYWNsIjogInByaXZhdGUiIH0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRDb250ZW50LVR5cGUiLCAiIl0sCiAgICBbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwgMCwgMTA0ODU3Nl0KICBdCn0K" /> So my Java Policy Code, lands me with an error Error> <Code>SignatureDoesNotMatch</Code> <Message> The request signature we calculated does not match the signature you provided. Check your key and signing method. .... I tried all possible ways to resolve this problem --Making sure the Policy Str and HTML fields match in content, order --Encoding the PolicyStr as suggested by Jboboli --Using the ServiceUtils class in JetS3t -- I also tried with<form action=" https://harvesttrial.s3.amazonaws.com/" -- followed the article by James http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1434&ref=featured but somehow something is missing. I am encoding my signature after that as follows log.debug("AWS Secret Key is $awsCredentials.secretKey") def policyB64 = getPolicy64Code("harvesttrial") String signature = ServiceUtils.signWithHmacSha1(awsCredentials.secretKey, policyB64); Please help |
|
|
Re: Using Jets3t to generate encoded Policy DocBTW, Here is my development setup
Using Groovy/Grails with JetS3T 0.6.1 (latest one)
|
|
|
Re: Using Jets3t to generate encoded Policy DocHi,
From a preliminary look at your code I have a few suggestions that may help: 1) Use the CNAME prefix format for the S3 bucket location, so your action parameter should be: http://harvesttrial.s3.amazonaws.com/ 2) Use a consistent case for the "Content-Type" parameter name. You are mixing "content-type" and "Content-Type", which may or may not matter. 3) Be very scrupulous about including double-quotes around your parameter values in the HTML form. Again, this may not matter, but if you don't have the quotes to explicitly delimit your values there is a risk that your browser may misinterpret your HTML. If it drops off or adds just 1 character for a value, it may cause the signature to fail. Hope this helps, James --- http://www.jamesmurty.com On Tue, Jan 6, 2009 at 8:39 PM, MarkAtHarvest <mark@...> wrote:
|
|
|
Re: Using Jets3t to generate encoded Policy DocJames, thank you very much for the urgent response.
After the changes suggested, the code just works awesome..cannot believe my eyes I would like to personally thank you for creating the Jets3t library, making the life of developers so easy and fruitful. (Its a request to add paypal button on your website, so that we can get the satisfaction of buying you a coffee) Here is the my final code, which rocks import org.jets3t.service.multithread.* import org.jets3t.service.impl.rest.httpclient.RestS3Service import org.jets3t.service.security.AWSCredentials import org.jets3t.service.model.S3Object import org.jets3t.service.model.S3Bucket import org.jets3t.service.acl.AccessControlList import org.jets3t.service.utils.ServiceUtils; import org.jets3t.service.Constant def getPolicy64Code(String bucketName) { Calendar cal = Calendar.instance; cal.add(Calendar.HOUR, 24); Date expiration = cal.getTime(); String dateStr = ServiceUtils.formatIso8601Date(expiration) String key = "mikey" def buckCond = S3Service.generatePostPolicyCondition_Equality("bucket", bucketName) def keyCond = S3Service.generatePostPolicyCondition("starts-with",'key', key) def aclCond = S3Service.generatePostPolicyCondition_Equality("acl", "private") //can be public-read def contCond = S3Service.generatePostPolicyCondition("starts-with",'Content-Type', "") def sizeCond = S3Service.generatePostPolicyCondition_Range(0, 1048576) def redirectCond = S3Service.generatePostPolicyCondition_Equality("success_action_redirect", "http://localhost:8080/") String[] conditions = [buckCond, keyCond, aclCond,contCond,sizeCond] String condStr = ServiceUtils.join(conditions,",") String policyDocument = "{\"expiration\": \"" + dateStr + "\", \"conditions\": ["+condStr + "]}" String policyB64 = ServiceUtils.toBase64(policyDocument.getBytes(Constants.DEFAULT_ENCODING)); log.debug(policyDocument) log.debug("Encrypted PolicyDoc is $policyB64") return policyB64 } //Corresponding Policy String is {"expiration": "2009-01-07T15:25:11.171Z", "conditions": [ {"bucket": "harvesttrial"}, ["starts-with", "$key", "mikey"], {"acl": "private"}, ["starts-with", "$Content-Type", ""], ["content-length-range", 0, 1048576]] } And the Corresponding HTML page is <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Upload File</title> </head> <body> <form action="http://harvesttrial.s3.amazonaws.com" method="post" enctype="multipart/form-data"> <input type="text" id="key" name="key" value="mikey.pdf"/> <input type="hidden" name="AWSAccessKeyId" value="${awsKey}" /> <input type="hidden" name="acl" value="private"> <input type="hidden" name="policy" value= "${policyB64}"/> <input type="hidden" name="signature" value= "${signature}" /> <input type="text" name="Content-Type" value="application/pdf" /> File to upload to S3: <input name="file" type="file"> <br> <input type="submit" value="Upload File to S3"> </br> </form> </body> </html>
|
|
|
Re: Using Jets3t to generate encoded Policy DocOops, I replied to my own message, please look at the reply above to follow the post
![]()
|
| Free embeddable forum powered by Nabble | Forum Help |