javascript - AWS S3 browser upload: Invalid Policy Invalid JSON error -
i want upload file s3 machine directly browser, bypassing sending server.
i've read relevant post amazon here, , full javascript example posted answer here. when attempt javascript solution receive following error:
<?xml version="1.0" encoding="utf-8"?> <error> <code>invalidpolicydocument</code> <message>invalid policy: invalid json.</message> <requestid>2f09c5449f42e3af</requestid> <hostid>m++sn+v5bjffkg0qa5cedves5bbsvjfhcxanlyf2ay11llld3eea54ct7rpg0cjhkla0itrnpgg=</hostid> </error>
on server side generate policy , sign using s3policy package edit: included signed policy example below:
var policy = {s3policybase64: "eyjlehbpcmf0aw9uijoimjaxni00ms0yovqxoji0oji4wiisim…swyjzdgfydhmtd2l0acisiirdb250zw50lvr5cguilciixv19", s3signature: "jlz6uddwvhhoeiu5urdttqupko0=", s3key: "akiaj6lfzzby24mbq4ga"}
here code i'm using in browser generate form , submit post:
var fd = new formdata(); // populate post paramters. fd.append('key', key); fd.append('content-type', content_type); fd.append('awsaccesskeyid', policy["s3key"]); fd.append('acl', 'private'); fd.append('success_action_redirect', "https://attachments.me/upload_callback") fd.append('policy', policy["s3policybase64"]) fd.append('signature',policy["s3signature"]) // file object retrieved file input. fd.append('file', $('#file_'+id)[0].files[0]); var xhr = new xmlhttprequest(); xhr.upload.addeventlistener("progress", uploadprogress, false); xhr.addeventlistener("load", uploadcomplete, false); xhr.addeventlistener("error", uploadfailed, false); xhr.addeventlistener("abort", uploadcanceled, false); xhr.open('post', dest_url, true); //must last line before send xhr.send(fd);
i've been through slow process of double checking outputs, i'm clueless causing invalid policy error. here base64 decoded version of policy, maybe identify invalid it.
{"expiration":"2016-41-29t1:24:28z", "conditions":[{"bucket":"intellilease-docs"},["eq","$key","uploads/ok/stephens/"],{"acl":"private"},["content-length-range",0,null],["starts-with","$content-type",""]]}
any or suggestions appreciated!
Comments
Post a Comment