Generating Amazon S3 file upload Policy in Node.js


I recently (by recently, I mean two days ago) had to upload files – images to be precise – to a newly created Amazon S3 bucket. After going through a few documents online and following instructions (at least, I think I did), I had my environment (MEAN – MongoDb, Express, AngularJS and Node.js) setup for the upload.

On trying the upload, Amazon returned an error telling me the policy I was using was not the same as that which they generated. Apparently, I did something wrong somewhere so I sought to right my wrong. I found a valuable script on github (though for the life of me, I can’t seem to remember the name of the project) and here’s what I distilled it down to:

To use this, I just issue a  GET  request of the form:  http://localhost:3000/s3_policy?subFolder=folder_name&mimeType=image/png

Creating Base64 Encoded String from an Image in Node.js


I’ve been writing a lot of MEAN (MongoDb, Express, AngularJS and Express) code lately and the latest in the series of hurdles I’ve been able to cross of recent has been generating the Base64 encoded version of an image file.

I had just added a new endpoint to an API I was working on and it included photos of users. For the web app, I was happy to just get the url of the photo and have the browser request it when needed but the API consumers (Android and iOS mobile devs) thought differently. The wanted to have the image as a base64 encoded string and so I reached out to Google.

Using this StackOverflow question and answer as my base, this is the function I eventually arrived at:

It should be noted that there are a few differences between my solution and that on StackOverflow.

  • First, I’ve created a function which expects a callback.
  • Secondly, line 6 makes use of  http.request  instead of  http.createClient  and  client.request . This is because  http.createClient  has been deprecated in my version of Express (4.0.0)