开发者

How to serve S3 files from a Rails app?

I am trying to allow users to download a S3 file by left clicking a link. Normally, they would have to do it by right-clicking and save-as.

I looked into http://apidock.com/rails/ActionController/DataStreaming/send_file but not sure if this is what I want.

This article http://www.jtricks.com/bits/content_disposition.html basically shows how it can be done by configuring Apache. However, we are using Heroku.

Anyone used Content-Disposition before to serve files from S3? Also wondering if this takes up a whole web process (Dyno)? Or if the whole process happens on the S3 server instead?

I tried:

send_file 'http://some_bucket_name.s3.amazonaws.com/uploads/users/28/songs/88/test.mp3', :type => 'audio/mp3', :di开发者_Python百科sposition => 'attachment'

And I get:

Cannot read file http://some_bucket_name.s3.amazonaws.com/uploads/users/28/songs/88/test.mp3

The file does exist. If I manually navigate to the url. The file plays fine.


This works for Rails 3. In your controller do:

response.headers['Content-Disposition'] = "attachment; filename=#{original_filename}"
self.response_body = proc { |response, output|
  AWS::S3::S3Object.stream(path, 'some_bucket_name') { |segment|
    output.write segment
  }
}

In your case:

original_filename = 'test.mp3'
path = '/uploads/users/28/songs/88/test.mp3'


try send_data AWS::S3.new.buckets['music'].objects["path/to/your/file.mp3"].read, filename: "some_file_name.mp3"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜