Zack Xu Zack Xu - 1 year ago 155
Ruby Question

How to convert base64 encoded string to HTTP::FormData::File in Ruby?

I have a base64 encoded string of a pdf file, which I can decode using Ruby


Now I want to convert it to an object of type
which I can then upload using http post to AWS S3.

How do I do this WITHOUT writing out the file to disk and reading it back in using"/path/to/file.pdf")

Thank you

Answer Source

I'm guessing you're using the http-form_data gem. In that case, your answer is in the very first example in the HTTP::FormData::File docs. For posterity:

Usage with StringIO

io = "foo bar baz" io, :filename => "foobar.txt"

And so:

require "stringio"
require "base64"

io =
file =, filename: "some_filename.txt")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download