Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 818
Minhal Shanjer Avatar asked Feb 08 '19 20:02

Minhal Shanjer


People also ask

What is active storage and how does it work?

It comes with a local disk-based service for development and testing and supports mirroring files to subordinate services for backups and migrations. Using Active Storage, an application can transform image uploads or generate image representations of non-image uploads like PDFs and videos, and extract metadata from arbitrary files.

How to get the path of a file in ActiveStorage?

The disk service implementation has a method called path_for that does what you're looking for but it is private. So using #send to get the paths or going through the download-to-temp-files process seem to be the options. ActiveStorage::Blob.service.path_for (user.avatar.key)

Why can’t active storage determine the content type of a file?

If you don’t provide a content type and Active Storage can’t determine the file’s content type automatically, it defaults to application/octet-stream. To remove an attachment from a model, call purge on the attachment.

What is the best way to store uploaded files?

Instead of storing uploaded files to disk, the best practice is to leverage a cloud file storage service such as Amazon’s S3. To use a different storage backend, you will need to modify the config/storage.yml file.


2 Answers

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.

like image 51
Jay Dorsey Avatar answered Sep 21 '22 09:09

Jay Dorsey


If you have variants:

variant = @employee_staff
            .avatar
            .attachment
            .variant(resize: '100x100')
            .processed # If variant is not processed

variant.service.send(:path_for, variant.key) # Absolute path to variant file
like image 44
hazg Avatar answered Sep 20 '22 09:09

hazg