Why does ActiveStorage create all these directories

This is the path to an image attachment in my app My question is why can’t they be in the same easier to find directory instead of places where you wouldn’t be able to find them without using <%= ActiveStorage::Blob.service.path_for(image.key) %>/<%= image.filename %> to find out what the path to the image file is /home/fugee/data/websites/fugeesite/storage/uk/rg/ukrgqd7u7pud52r54d9eis3rq7gr/IMG_20230519_225450994.jpg

This is common practice for storage systems that have to handle a large amount of files, and it’s mostly due to performance: too many files in a single directory will make the file system take longer to locate the files which would worsen the performance of your application.

Also notice the the first directory uses the first two letters of the key, and the second directory uses the next two. So it’s not completely random.

3 Likes

If I have has_many_attached :images then “image” is the same as attachment for the purpose of discussion and image has both an id and a key? I’m able to use image.id in my code

Every file uploaded creates an attachment and a blob. The attachment is the join table between the blob and your record, and the blob is what contains the information to locate the file itself in the storage.

Blob has both a key and an ID, but usually you only use the ID. The key is pretty much something to be used internally by active storage, not by you in your application. Most of the time you won’t be using the ID either:

# Display image
<%= image_tag user.avatar %>

# File download
<% link_to rails_blob_path(user.avatar, disposition: "attachment") %>

Thanks Here’s what my problem is, my in house web app keeps track of my inventory of things I can sell on eBay If I decide to list something on eBay I have the images as attachments to the items in my app and need to upload them to eBay so then I have to go through all these crazy folders, I don’t like it at all I understand too many files in a directory can be a problem but this is pathetic

@fugee_ohu You can create a block of code to help you with that.

class ActiveStorage::File
  def self.move_to_desktop(item)
    blob.open(tmpdir: Rails.root.join("tmp")) do |file|
      FileUtils.mv file.path, File.expand_path('~/Desktop')
    end  
  end
end

Then supposing you have this class:

class Product < ApplicationRecord
  has_many_attached :images
end

You can do this to copy the files to your desktop:

product = Product.find ID
product.images.each { |image| ActiveStorage::File.move_to_desktop image }
2 Likes

That’s great thanks It turns out the directory was protected I couldn’t see files when logged in from a remote Nemo file manager connection “Files->Connect to server”

Currently, the image files are scattered across various storage locations, and it’s becoming quite a challenge to locate them without resorting to complex methods like <%= ActiveStorage::Blob.service.path_for(image.key) %>/<%= image.filename %> . It got me thinking, wouldn’t it be more logical to have all these image attachments stored in a more unified and easily accessible directory? This would greatly simplify the process of finding and managing these images. The existing approach feels convoluted and not very user-friendly.