Rails 6 adds ActiveStorage::Blob#open which downloads a blob to a tempfile on disk and yields the tempfile.
Processing a blob
Let’s take an example of a face detection application where the user images are uploaded. Let’s assume that the images are uploaded on S3.
Before Rails 6, we will have to download the image in system’s memory, process it with an image processing program and then send the processed image back to the S3 bucket.
If the processing operation is successful, the original file can be deleted from the system. We need to take care of a lot of uncertain events from the download phase till the phase when the processed image is created.
ActiveStorage::Blob#open to the rescue
ActiveStorage::Blob#open, abstracts away all this complications and gives us a tempfile which is closed and unlinked once the block is executed.
open takes care of handling all the fanfare of getting a blob object to a tempfile.
open takes care of the tempfile cleanup after the block.
tempfiles are created
also takes an optional argument
tmpdir to set
a custom directory
for storing the tempfiles.
Here is the relevant commit.