Uploading Base64 image data to S3 via AWS Ruby SDK


I’ve got a drag and drop function which takes the file that’s been dropped on it and converts it to Base64 data. Before, it was uploading to Imgur, whose API supports Base64 uploads, and now I’m working on moving to Amazon S3.

I’ve seen examples of people using XMLHTTP requests and CORS to upload data to S3, I’m using Amazon’s AWS S3 SDK gem to avoid having to sign policies and other things, as the gem does that for me. So what I’ve done is send the Base64 data to a local controller metod which uses the gem to upload to S3.

The other posts using Ajax i’ve seen show that S3 supports raw data uploads, but the gem doesn’t seem to, as whenever I view the uploads i get broken images. Am I uploading it incorrectly? Is the data in the wrong format? I’ve tried the basic Base64, atob Base64, and blob urls, but nothing works so far.


Controller method:


To be clear, I’ve tried a couple of different formats, the one displayed above is decoded base64. Regular Base64 looks like this:

and a blob url looks like this:


Am I reading this right that you are:

  1. Using AJAX to send a Base64-encoded file to Rails
  2. Using Rails to upload the file to S3
  3. Viewing the file in S3?

If that’s the case, you need to decode the data in step 2 before sending it on to S3. Something like this might work:

Leave a Reply