4

Well my problem is that I'm using send_data on my Rails 3 application to send to the user a file from AWS S3 service with something like

Base.establish_connection!( :access_key_id => 'my_key', :secret_access_key => 'my_super_secret_key')
s3File = S3Object.find dir+filename, "my_unique_bucket"
send_data(open(s3File.url).read,:filename=>filename, :disposition => 'attachment')

but seems like the browser is buffering the file and before buffering it sends the file to download taking no time on the download but at the buffering time it's taking as long as the file size .... but what i need is the user to view the download process as normal, they won't know what happening with the loader only on the browsers tab:

enter image description here

They'd rather see a download process i guess to figure out there's something happening there

is there any way i can do this with send_data?

Mr_Nizzle
  • 6,644
  • 12
  • 55
  • 85

2 Answers2

6

It's not the browser that's buffering/delaying, it's your Ruby server code.

You're downloading the entire file from S3 before sending it back to the user as an attachment.

It may be better to serve this content to your user directly from S3 using a redirect. Here's a link to building temporary access URLs that will allow a download with a given token for a short period of time:

http://docs.amazonwebservices.com/AmazonS3/latest/dev/S3_QSAuth.html

Base.establish_connection!( :access_key_id => 'my_key', :secret_access_key => 'my_super_secret_key')
s3File = S3Object.find dir+filename, "my_unique_bucket"
redirect_to s3File.url(:expires_in => 30)
Winfield
  • 18,985
  • 3
  • 52
  • 65
  • nice ! and i found this `redirect_to S3Object.url_for(dir+filename,'my_unique_bucket',:expires_in => 60,:use_ssl => true)` now it's only two lines including the `establish_connection` line – Mr_Nizzle Sep 07 '11 at 17:49
-1

Set Your Content Disposition

You'll need to set the content-disposition of the S3 url for it download instead of opening up in the browser. Here is my basic implementation:

Think of attachment as your s3file.

In your attachment.rb

def download_url
  s3 = AWS::S3.new.buckets[ 'bucket_name' ]

  s3.url_for( :read,
    expires_in: 60.minutes, 
    use_ssl: true, 
    response_content_disposition: "attachment; filename='#{file_name}'" ).to_s
end

In your views

<%= link_to 'Download Avicii by Avicii', attachment.download_url %>

Thanks to guilleva for his guidance.

Community
  • 1
  • 1
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245