Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I customize the path of a Rails 5.2 ActiveStorage attachment in Amazon S3?

When adding attachments such as

has_one_attached :resume_attachment

saved files end up in the top level of the S3 bucket. How can I add them to subdirectories? For example, my old paperclip configuration could categorize in directories by model name.

like image 269
Peter DeWeese Avatar asked Apr 18 '18 13:04

Peter DeWeese


People also ask

What is Activestorage?

Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects.

What is active storage blob?

A blob is a record that contains the metadata about a file and a key for where that file resides on the service. Blobs can be created in two ways: Subsequent to the file being uploaded server-side to the service via create_after_upload!.


1 Answers

You can not. There is only one option possible, at that time, for has_one_attached, has_many_attached macros that is :dependent. https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/attached/macros.rb#L30

see (maybe the reason why you have downvotes, but it is about "direct" upload so...) : How to specify a prefix when uploading to S3 using activestorage's direct upload?. The response is from the main maintainer of Active Storage.

like image 96
Dinatih Avatar answered Oct 14 '22 04:10

Dinatih