Zack Xu Zack Xu - 7 months ago 77
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


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")