Olivier
Olivier

Reputation: 543

Change default url from Active Storage

Can we change the default 'permanent' url create from active storage to redirect to S3. Is something like rails/active_storage/representations/. I don't like the framework name in the url.

Thanks

Upvotes: 7

Views: 5796

Answers (1)

stwienert
stwienert

Reputation: 3632

UPDATE: Recently, there was an addition which makes the route prefix configurable in Rails 6: https://guides.rubyonrails.org/6_0_release_notes.html#active-storage-notable-changes

It's just a matter of configuration:

Rails.application.configure do
  config.active_storage.routes_prefix = '/whereever'
end

Unfortunately, the url is defined in ActiveStorage routes.rb without easy means to change:

get "/rails/active_storage/blobs/:signed_id/*filename" => 
     "active_storage/blobs#show", as: :rails_service_blob
get "/rails/active_storage/representations/:signed_blob_id/:variation_key/*filename" => 
     "active_storage/representations#show", as: :rails_blob_representation

One solution starting point I can think of is defining your own Routes in addition and overriding the "rails_blob_representation_path" or similar

get "/my_uploads/:signed_blob_id/:variation_key/*filename" => 
  "active_storage/representations#show", as: :my_upload

and then overriding the path in a helper file and include the helper into the Named Helpers:

How to override routes path helper in rails?

module CustomUrlHelper
  def rails_blob_representation(*args)
    my_upload(*args)
  end
end

# initializer etc.
Rails.application.routes.named_routes.url_helpers_module.send(:include, CustomUrlHelper)

The solution might need some adjustments though, I didn't tested it.

Upvotes: 10

Related Questions