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.
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.
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)
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.
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.
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.
Inside the subfolder is a file with the name that matches the key you printed out above (no extension). That's your file.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With