Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write blob data to a file with ruby?

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?

like image 773
AKWF Avatar asked Apr 18 '14 15:04

AKWF


People also ask

How do I convert blob data to a file?

Files are Blobs, just tack on the meta properties and you're good to go. The default for a blob when uploading it is blob . So, I first extracted the name of the file I was cropping and then gave the same filename so the cropped file while uploading it to server by doing form. append("blob",blob, filename); .

What is file type blob?

BLOB stands for a “Binary Large Object,” a data type that stores binary data. Binary Large Objects (BLOBs) can be complex files like images or videos, unlike other data strings that only store letters and numbers. A BLOB will hold multimedia objects to add to a database; however, not all databases support BLOB storage.

What are active storage blobs?

Class ActiveStorage::Blob < ActiveRecord::Base. A blob is a record that contains the metadata about a file and a key for where that file resides on the service. Blobs can be created in two ways: Subsequent to the file being uploaded server-side to the service via create_after_upload! .


1 Answers

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
like image 166
Chris Cashwell Avatar answered Sep 28 '22 01:09

Chris Cashwell