Search code examples
ruby-on-railsrubyrails-activestorage

Where does active storage store files (on disk), and how can I retrieve them physically?


I am using active storage with Rails 5.2. I am following the EdgeRails guide, and have configured Active-Storage to use the local disk.

The file uploads work great when I am using the Rails App.

However, the problem is that I need to physically access those uploaded files without using Rails as a mediator.

A query for where the files are stored returns this:

url_for(@employee_staff.avatar)
=> "/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBGUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e76664d247cb5437fe1cd11f7ee0ded24f95aee2/profilepic3.jpeg"

I am trying to figure out where this file path is saved in my local disk. So far, I've had no luck.

Any explanations about how Active-Storage works and where I can see the uploaded files are greatly appreciated.


Solution

  • On your local development machine (since you mentioned local disk), you should have a file config/storage.yml that has a block similar to below:

    local:
      service: Disk
      root: <%= Rails.root.join('storage') %>
    

    In this example above, I'm storing the files in a folder named storage in the root of my rails application. Inside that folder you'll find nested folders that aren't meant to be navigated via file explorer/Finder (but you can).

    Thru your rails app, via views for example, you'd use the url helpers which aren't well documented yet.

    From a rails console, you can try, for a model named Foo, with a has_one_attached :photo

    Foo.last.photo.blob.key
    

    It should give you a ~24 character string.

    • The first 2 characters are a subfolder inside the folder I pointed you to above
    • The next 2 characters are a subfolder inside that

    Inside the subfolder is a file with the name that matches the key you printed out above (no extension). That's your file.