1
votes

I save image attachments with the paperclip gem in my rails 2.3 app to AWS S3. I would like to be able to allow the user to download the image attachment as an actual download instead of opening a new browser tab. When I saved the image attachment locally on the server I used:

picture_filename = RAILS_ROOT + '/public' + params[:picture_filename]
send_file(picture_filename, :type => 'text', :stream => "false", :disposition => 'attachment')

However, send_file does not work if the location is on aws-s3.

How is this achieved with the aws-sdk gem?

1

1 Answers

7
votes

If you have your s3 config setup up on your paperclip model correctly

such as

has_attached_file :picture,
:storage => :s3,
:url => ":s3_domain_url",
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "picture/:id/:filename"

Then in your controller action should be able to go something like

@pic = Picture.find(1)

send_file(@pic.picture.to_file, :type => @pic.picture_content_type, :disposition => "attachment", :filename => @pic.picture_file_name)

This should get the correct aws s3 path. Note the .to_file this was removed in later versions of paperclip but if you are using rails 2.3 you might have an earlier version.

Im currently using this to download using rails 3.2+ and paperclip 3.0+

send_file(Paperclip.io_adapters.for(@pic.picture).path, :type => @pic.picture_content_type, :disposition => "attachment", :filename => @pic.picture_file_name)