Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Active Storage set folder to store files

I'm using Active Storage to store files in a Rails 5.2 project. I've got files saving to S3, but they save with random string filenames and directly to the root of the bucket. I don't mind the random filenames (I actually prefer it for my use case) but would like to keep different attachments organized into folders in the bucket.

My model uses has_one_attached :file. I would like to specify to store all these files within a /downloads folder within S3 for example. I can't find any documentation regarding how to set these paths.

Something like has_one_attached :file, folder: '/downloads' would be great if that's possible...

like image 816
Ryan Avatar asked Jun 17 '18 03:06

Ryan


People also ask

Where are active storage files stored?

By default in the development environment, Active Storage stores all uploaded images on your local disk in the storage subdirectory of the Rails application directory. That's the file you uploaded!


3 Answers

There is no official way to change the path which is determined by ActiveStorage::Blob#key and the source code is:

def key
  self[:key] ||= self.class.generate_unique_secure_token
end

And ActieStorage::Blog.generate_unique_secure_token is

def generate_unique_secure_token
  SecureRandom.base36(28)
end

So a workaround is to override the key method like the following:

# config/initializers/active_storage.rb
ActiveSupport.on_load(:active_storage_blob) do
  def key
    self[:key] ||= "my_folder/#{self.class.generate_unique_secure_token}"
  end
end

Don't worry, this will not affect existing files. But you must be careful ActiveStorage is very new stuff, its source code is variant. When upgrading Rails version, remind yourself to take look whether this patch causes something wrong.

You can read ActiveStorage source code from here: https://github.com/rails/rails/tree/master/activestorage

like image 74
Yi Feng Xie Avatar answered Oct 15 '22 04:10

Yi Feng Xie


The ultimate solution is to add an initializer. You can add a prefix based on an environment variable or your Rails.env :

# config/initializer/active_storage.rb
Rails.configuration.to_prepare do
    ActiveStorage::Blob.class_eval do
      before_create :generate_key_with_prefix
  
      def generate_key_with_prefix
        self.key = if prefix
          File.join prefix, self.class.generate_unique_secure_token
        else
          self.class.generate_unique_secure_token
        end
      end
  
      def prefix
        ENV["SPACES_ROOT_FOLDER"]
      end
    end
  end

It works perfectly with this. Other people suggest using Shrine.

Credit to for this great workaround : https://dev.to/drnic/how-to-isolate-your-rails-blobs-in-subfolders-1n0c

like image 40
ZazOufUmI Avatar answered Oct 15 '22 04:10

ZazOufUmI


As of now ActiveStorage doesn't support that kind of functionality. Refer to this link. has_one_attached just accepts name and dependent.

Also in one of the GitHub issues, the maintainer clearly mentioned that they have clearly no idea of implementing something like this.

The workaround that I can imagine is, uploading the file from the front-end and then write a service that updates key field in active_storage_blob_statement

like image 40
Abhilash Reddy Avatar answered Oct 15 '22 05:10

Abhilash Reddy