---
title: "Rails 6 adds ActiveStorage::Blob#open"
description: "Rails 6 adds ActiveStorage::Blob#open"
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-activestorage-blob-open"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-activestorage-blob-open.md"
---

# Rails 6 adds ActiveStorage::Blob#open

Rails 6 adds ActiveStorage::Blob#open

- Author: Akhil Gautam
- Published: October 30, 2019
- Categories: Rails 6, Rails

Rails 6 adds
[ActiveStorage::Blob#open](https://edgeapi.rubyonrails.org/classes/ActiveStorage/Blob.html#method-i-open)
which downloads a blob to a tempfile on disk and yields the tempfile.

```ruby
>> blob = ActiveStorage::Blob.first
=> <ActiveStorage::Blob id: 1, key: "6qXeoibkvohP4VJiU4ytaEkH", filename: "Screenshot 2019-08-26 at 10.24.40 AM.png", ..., created_at: "2019-08-26 09:57:30">

>> blob.open do |tempfile|
>>   puts tempfile.path  #do some processing
>> end
# Output: /var/folders/67/3n96myxs1rn5q_c47z7dthj80000gn/T/ActiveStorage-1-20190826-73742-mve41j.png
```

### 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.

#### The overhead

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.

&nbsp;1. `open` takes care of handling all the fanfare of getting a blob object
to a tempfile. &nbsp;2. `open` takes care of the tempfile cleanup after the
block.

```ruby

> > blob = ActiveStorage::Blob.first
> > blob.open do |tempfile|
> > tempfile #do some processing
> > end

# once the given block is executed

# the tempfile is closed and unlinked

=> #<Tempfile: (closed)>
```

By default, tempfiles are created in `Dir.tmpdir` directory, but
ActiveStorage::Blob#open also takes an optional argument `tmpdir` to set a
custom directory for storing the tempfiles.

```ruby

> > Dir.tmpdir
> > => "/var/folders/67/3n96myxs1rn5q_c47z7dthj80000gn/T"

> > blob = ActiveStorage::Blob.first
> > blob.open(tmpdir: "/desired/path/to/save") do |tempfile|
> > puts tempfile.path #do some processing
> > end
> >
```

Here is the relevant
[commit](https://github.com/rails/rails/commit/9f95767979579f5761cb0d2bcccb67f3662349c5).

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-activestorage-blob-open)
