ruby-on-railsurlrails-activestorage

How to set expiry for ActiveStorage URLs?


So according to the official Rails docs, the URLs for viewing the stored files are permanent and there is no way to expire them.

The URLs that are generated using rails_blob_path and rails_storage_proxy_path.

If someone knows blob_id and filename of an object, the URLs can be generated.

Is there a workaround to set the expiry?


Solution

  • The following would be helpful:

    # signed URL with expiration
    rails_blob_path(user.avatar, disposition: "attachment", expires_in: 10.minutes)
    
    # or
    
    # set a default expiration time in config/application.rb
    module Blog
      class Application < Rails::Application
        config.active_storage.urls_expire_in = 30.minutes
      end
    end
    

    For more details, you can visit here