Change default url from Active Storage

允我心安 提交于 2019-12-09 06:45:43

问题


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


回答1:


UPDATE: Recently, there was an addition which makes the route prefix configurable: https://github.com/rails/rails/commit/7dd9916c0d5e5d149bdde8cbeec42ca49cf3f6ca

Just in master branch now, but should be integrated into ~> 5.2.2 and higher.

Then, 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.



来源:https://stackoverflow.com/questions/50128302/change-default-url-from-active-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!