Active Storage provide filename for use where file is actually stored

I know this isn’t how AS is designed but I wish to have the files stored on S3 with a particular filename so i can easily when needed download them in bulk from S3 and cross reference their filenames with a spreadsheet I have.

When uploading a file I need the source filename to be set to a slugged version of the name attribute of the record it’s being attached to.

If the record is called “Castle in Scotland”, then I wish the filename on S3 to be called “castle-in-scotland.jpg”.

How can I do that?

Haven’t tested it, but this PR seems to be what you want:

What would you expect to happen if another user uploads a different image with the same name?

One of the benefits of the generated keys used by ActiveStorage is avoiding this kind of collision. And even in the example in George’s key override PR the provided overwritten key uses “avatar/#{user.id}.jpg” which defines the uniqueness of that file very clearly.

Beware collisions is all. Cheers.

George Claghorn’s solution relies on .attach(...). How to specify a custom blob key when uploading the file through a form?

<%= form.file_field :avatar %>