Question

I'm trying to download all of our information off of a really lousy cloud server. The files are images and PDFs. My problem is that I don't know how to write the blob data that I receive from the read_object call I do via this cloud API out to a file on my local filesystem.

I know that I could use ImageMagick/RMagick to create an image from the blob, but I would rather just skip this step and write the data directly to a file. I don't want to have to worry about having ImageMagick compiled with every single decode delegate ever.

I don't really see much information on Google for this, is this something that is just not done often with Ruby?

Était-ce utile?

La solution

Assuming the file doesn't exist or you want to overwrite its current content, you just need to open the file with mode wb (w for write, b for binary — the b may not be strictly necessary on all systems). If you're trying to append to a file, use ab instead. See this page for more information on modes.

File.open(filename, 'wb') do |f|
  f.write blob
end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top