23

When Active Storage creates a signed variant URL, it sets a default timeout of 5.minutes. I really want to increase this, but I've been trawling Github issues, code diving and cannot find it anywhere. On line 44 of the services class a class_attribute is set, but how can this be overwritten?

https://github.com/rails/rails/blob/5-2-stable/activestorage/lib/active_storage/service.rb#L44

I'm using url_for to generate the signed variant links and there doesn't seem to be anyway to change the setting then. Any help would be greatly appreciated.

Thank you! :)

Paul Danelli
  • 994
  • 1
  • 15
  • 25
  • what do you think about inheritance? from service.rb class and override the value of url_expires_in? – Nezir Sep 29 '18 at 20:19

1 Answers1

59

Set ActiveStorage::Service.url_expires_in directly, e.g. in an initializer:

# config/initializers/active_storage.rb
ActiveStorage::Service.url_expires_in = 1.hour

Rails 6 will add config.active_storage.service_urls_expire_in:

# config/initializers/active_storage.rb
Rails.application.config.active_storage.service_urls_expire_in = 1.hour
equivalent8
  • 13,754
  • 8
  • 81
  • 109
George Claghorn
  • 26,261
  • 3
  • 48
  • 48
  • 2
    Sir is there a limit for this configuration? Say can you do a `5.months` duration config? – Ricardo Green Apr 01 '19 at 09:08
  • @RicardoGreen According to https://github.com/rails/rails/issues/31581 there is a limit of 1 week if you use S3. – Henrik N May 03 '19 at 22:12
  • @George Claghorn How to generate active_storage.rb file? I don't have active_storage.rb file in my hole project. – AGM May 28 '20 at 14:17
  • You would just create the file in your project folder. Use the path given in the comments config/initializers/active_storage.rb. Alternatively you can add this to your development.rb or production rb environment files as well. – Marcus Salinas Aug 01 '20 at 18:45
  • 6
    How do you create a non-expiring link to the file? – Corey Nov 30 '20 at 20:09
  • 3
    @Corey You can now setup permanent links by setting up a public bucket. https://edgeguides.rubyonrails.org/active_storage_overview.html#public-access – Matthew Lemieux Mar 30 '21 at 20:08