I have a download link in my app from which users should be able to download files which are stored on s3. These files will be publicly accessible on urls which look somethi
I think the best way to handle this is using an expiring S3 url. The other methods have the following issues:
send_data doesn't produce the expected "browser download".download controller action.My implementation looks like this:
attachment.rbdef download_url
S3 = AWS::S3.new.buckets[ 'bucket_name' ] # This can be done elsewhere as well,
# e.g config/environments/development.rb
url_options = {
expires_in: 60.minutes,
use_ssl: true,
response_content_disposition: "attachment; filename=\"#{attachment_file_name}\""
}
S3.objects[ self.path ].url_for( :read, url_options ).to_s
end
<%= link_to 'Download Avicii by Avicii', attachment.download_url %>
That's it.
If you still wanted to keep your download action for some reason then just use this:
In your attachments_controller.rb
def download
redirect_to @attachment.download_url
end
Thanks to guilleva for his guidance.