Search code examples
ruby-on-railsruby-on-rails-3amazon-s3carrierwavefog

How do I dynamically generate a download link for file uploaded to S3 with Carrierwave?


I have 2 models that I want CarrierWave to interact with. Item and Image models.

What I want to happen is once the user uploads their item to S3 via Carrierwave - that part is pretty straight forward - whenever another user wants to download the item, they press a button that triggers some method that dynamically generates a download link - that expires when 2 conditions are met. Either the item is downloaded X number of times, or X hours have passed (say 24 hours).

The idea being that there isn't a static download link floating around on the internet to that file.

How do I do that?


Solution

  • CarrierWave allows you to set the fog_public and fog_authenticated_url_expiration options both for every uploader (through an initializer) or on a specific uploader. In the latter case you just place self.fog_public = false and self.fog_authenticated_url_expiration = 123 inside your uploader class definition. With these two options set, any calls to model.uploader.url will return a specially built URL that will expire after the set amount of time.