16

Right now I am using Amazon S3 and Paperclip which is allowing my users to upload an image that is associated with the event they are creating. My ultimate goal is since others can view this event, to be able to click on the image and have it prompt a SAVE TO their computer. As of now, clicking the link will open the image in a browser window. I rather have it ask for them to download instead. All images only saved on S3, not local. Need to hide exposed s3 url as well if possible or camouflage it

Here is my current setup

Index.html

<%= link_to 'Download Creative', event.creative.url, class: "btn btn-info" %>

Event.rb

has_attached_file :creative,
                :styles => { :thumb => "150x150", :custcreative => "250x75" },
                :path => ":attachment/:id/:style.:extension",
                :s3_domain_url => "******.s3.amazonaws.com",
                :storage => :s3,
                :s3_credentials => Rails.root.join("config/s3.yml"),
                :bucket => '*****',
                :s3_permissions => :public_read,
                :s3_protocol => "http",
                :convert_options => { :all => "-auto-orient" },
                :encode => 'utf8'

Hoping someone can help me out.

RubyNewbie
  • 547
  • 5
  • 21

5 Answers5

30

To avoid extra load to your app (saving dyno's time in Heroku), I would rather do something like this: add this method to your model with the attachment:

def download_url(style_name=:original)
  creative.s3_bucket.objects[creative.s3_object(style_name).key].url_for(:read,
      :secure => true,
      :expires => 24*3600,  # 24 hours
      :response_content_disposition => "attachment; filename='#{creative_file_name}'").to_s
end

And then use it in your views/controllers like this:

<%= link_to 'Download Creative', event.download_url, class: "btn btn-info" %>
guilleva
  • 1,301
  • 11
  • 13
  • 1
    Genius! This worked wonderfully for downloading right from S3, not tying up the Ruby process, not downloading to the server first, and not sending the user away. Awesome – Joshua Pinter Apr 18 '14 at 19:36
  • Plus, it doesn't require another action in the controller. – Joshua Pinter Apr 18 '14 at 19:36
  • 1
    Read a lot of other answers for downloading files from s3 (send_data and send_file solutions everywhere), but this was most concise and just straight up worked right of the bat with least amount of code. – Sean Montana May 28 '15 at 16:27
  • Can you please explain what is creative and s3_object() .Also what is style_name. Actually I am getting error undefined method creative – Aniket Tiwari Oct 04 '17 at 12:34
  • "creative" is how the attachment is named (see the question), s3_object is a Paperclip method to obtain a reference of a S3 object and the optional style_name param is just in case you want to create a link for a different version (usually a thumbnail or resized image) instead of the original file. You have to replace "creative" with the name of your attachment – guilleva Oct 04 '17 at 14:32
17

To make this work, I've just added a new action in the controller, so in your case it could be:

#routes
resources :events do
  member { get :download }
end

#index
<%= link_to 'Download Creative', download_event_path(event), class: "btn btn-info" %>

#events_controller
def download
  data = open(event.creative_url)
  send_data data.read, :type => data.content_type, :x_sendfile => true
end

EDIT: the correct solution for download controller action can be found here (I've updated the code above): Force a link to download an MP3 rather than play it?

Community
  • 1
  • 1
santuxus
  • 3,662
  • 1
  • 29
  • 35
3

Now in aws-sdk v2, there is a method :presigned_url defined in Aws::S3::Object, you can use this method to construct the direct download url for a s3 object:

s3 = Aws::S3::Resource.new
# YOUR-OBJECT-KEY should be the relative path of the object like 'uploads/user/logo/123/pic.png'
obj = s3.bucket('YOUR-BUCKET-NAME').object('YOUR-OBJECT-KEY')
url = obj.presigned_url(:get, expires_in: 3600, response_content_disposition: "attachment; filename='FILENAME'")

then in your views, just use:

= link_to 'download', url
Jonas W
  • 3,200
  • 1
  • 31
  • 44
Jimmy Huang
  • 4,252
  • 2
  • 22
  • 22
1
event = Event.find(params[:id])
  data = open(event.creative.url)
  send_data data.read, :type => data.content_type, :x_sendfile => true, :url_based_filename => true
end
RubyNewbie
  • 547
  • 5
  • 21
0

You need to set the "Content-Disposition" to "attachment" in your HTTP response header. I'm not a Rails developer - so just Google it and you'll see plenty of examples - but it probably looks something like this:

    :content_disposition => "attachment"

or

     ...
    :disposition => "attachment"
user553180
  • 626
  • 5
  • 6