amazon s3 - Browser upload direct to s3, barebones Node.js -
found writeup on heroku on uploading private s3 buckets, direct browsers:
https://devcenter.heroku.com/articles/s3-upload-node
for me, aws-sdk package got in way more helped. dubious s3upload script concern.
how 1 upload directly s3 without cruft in way?
had dig old java-based project figure 1 out. turns out it's nice , simple once down essentials. first, setup quick html form upload:
<form action="https://bucketname.s3.amazonaws.com/" method="post" enctype="multipart/form-data"> <input type="hidden" name="key" value="key"></input> <input type="hidden" name="awsaccesskeyid" value="access_key"></input> <input type="hidden" name="policy" value="policy"></input> <input type="hidden" name="signature" value="signature"></input> <input type='file' name='file'> <input type='submit' value='upload driver photo'> </form>
next, cook quick node.js script generate values policy
, signature
above, encodedpolicy
, signature
variables below. remember, both values must base64 encoded:
var crypto=require("crypto"), bucketname="bucketname", secretkey="secret_key", s3key="key", expiration= new date(); expiration.setdate(expiration.getdate()+1); var policy={ expiration:expiration.toisostring(), conditions:[ {bucket:bucketname}, {key:s3key} ] }, encodedpolicy=new buffer(json.stringify(policy)).tostring("base64"), signature=crypto.createhmac( "sha1", secretkey ).update(encodedpolicy).digest("base64");
wrap node.js code above in promise tastiness. embed in angular/rest, express templating, or other middleware/presentation framework of choice.
please note: code above not limit upload file size. done condition, i.e. 50mb limit: ["content-length-range", 0, 52428800]
.
Comments
Post a Comment