如何将base64编码的字符串转换为Ruby中的HTTP::FormData::File?

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

我有一个 base64 编码的 pdf 文件字符串,我可以使用 Ruby

对其进行解码
Base64.decode(the_string)

现在我想将其转换为 HTTP::FormData::File 类型的对象,然后我可以使用 http post 将其上传到 AWS S3。

我该怎么做 WITHOUT 将文件写入磁盘并使用

读回
HTTP::FormData::File.new("/path/to/file.pdf")

谢谢

我猜你正在使用 http-form_data gem. In that case, your answer is in the very first example in the HTTP::FormData::File docs。为了后代:

Usage with StringIO

io = StringIO.new "foo bar baz"
FormData::File.new io, :filename => "foobar.txt"

等等:

require "stringio"
require "base64"

io = StringIO.new(Base64.decode64(the_string))
file = HTTP::FormData::File.new(io, filename: "some_filename.txt")