Change default url from Active Storage

后端 未结 1 401
礼貌的吻别
礼貌的吻别 2021-02-06 10:34

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 na

1条回答
  •  情歌与酒
    2021-02-06 11:33

    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.

    0 讨论(0)
提交回复
热议问题