# \[Proposal\] ActiveStorage - Allow a prefix to be supplied with a direct upload

**URL:** <https://discuss.rubyonrails.org/t/proposal-activestorage-allow-a-prefix-to-be-supplied-with-a-direct-upload/84108>\
**Category:** rubyonrails-talk\
**Tags:** feature\
**Created:** [November 4, 2023, 1:03am UTC](https://discuss.rubyonrails.org/t/proposal-activestorage-allow-a-prefix-to-be-supplied-with-a-direct-upload/84108 "2023-11-04T01:03:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![garden\_gnome](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@garden\_gnome](https://discuss.rubyonrails.org/u/garden_gnome)\
**Post date:** [November 4, 2023, 1:03am UTC](https://discuss.rubyonrails.org/t/proposal-activestorage-allow-a-prefix-to-be-supplied-with-a-direct-upload/84108/1 "2023-11-04T01:03:54Z")

</div>

I would like to specify a prefix to files when they are being directly uploaded by a user.

This has been mentioned a handful of times rails/rails/issues/44376 and ROR Discussion - activestorage-and-direct-uploading/79832

In my use case these direct uploads are not the primary / preferred path to acquire information. In the preferred path I’m pulling records and storing them with a prefix that makes a tiny bit of sense to a human and I’d prefer to maintain that structure with direct uploads.

I’m thinking the change could look something like:

- Allow a `prefix` to be specified in the [DirectUpload constructor](https://github.com/rails/rails/blob/a6099ed2b7a544d8fe67688e8a0f9237847404ee/activestorage/app/javascript/activestorage/direct_upload.js#L8)
- Pass the `prefix` through to the [BlobRecord constructor and attributes](https://github.com/rails/rails/blob/a6099ed2b7a544d8fe67688e8a0f9237847404ee/activestorage/app/javascript/activestorage/blob_record.js#L7C10-L7C20)
- Add `prefix` to the permitted blob args in the [DirectUpload controller](https://github.com/rails/rails/blob/a6099ed2b7a544d8fe67688e8a0f9237847404ee/activestorage/app/controllers/active_storage/direct_uploads_controller.rb#L14)
- Add `prefix` to the args in [ActiveStorage::Blob.create\_before\_direct\_upload](https://github.com/rails/rails/blob/d0f40f71b0b61f828291f366bad5d29ab44e70d2/activestorage/app/models/active_storage/blob.rb#L109)

At this point something would need to be done with the [Key attribute in ActiveStorage::Blob](https://github.com/rails/rails/blob/d0f40f71b0b61f828291f366bad5d29ab44e70d2/activestorage/app/models/active_storage/blob.rb#L24C3-L24C20) as that is currently `has_secure_token :key, length: MINIMUM_TOKEN_LENGTH`

Perhaps a `before_create` that joins a prefix if provided.

I haven’t tried implementing this as I wanted to see if there is a general appetite for this change as I hate for a PR to languish or get rejected because I’ve missed something.

Any interest?

---

<div class="post-metadata">

**Author:** ![Intrepidd](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/intrepidd/32/10900_2.png) [@Intrepidd](https://discuss.rubyonrails.org/u/Intrepidd)\
**Post date:** [November 28, 2023, 2:17pm UTC](https://discuss.rubyonrails.org/t/proposal-activestorage-allow-a-prefix-to-be-supplied-with-a-direct-upload/84108/2 "2023-11-28T14:17:38Z")

</div>

I’m looking for this as I’m trying to migrate from Shrine and my files are already uploaded in a prefixed location.
