如何从 Rails 应用程序提供 S3 文件?
我试图允许用户通过左键单击链接来下载 S3 文件。通常,他们必须通过右键单击并另存为来完成此操作。
我查看了 http://apidock.com/rails/ActionController/DataStreaming/send_file 但是不确定这是否是我想要的。
这篇文章 http://www.jtricks.com/bits/content_disposition.html 基本上展示了如何通过配置 Apache 来完成此操作。然而,我们正在使用 Heroku。
有人以前使用过 Content-Disposition 来提供 S3 中的文件吗?还想知道这是否占用了整个网络进程(Dyno)?或者如果整个过程发生在 S3 服务器上呢?
我尝试过:
send_file 'http://some_bucket_name.s3.amazonaws.com/uploads/users/28/songs/88/test.mp3', :type => 'audio/mp3', :disposition => 'attachment'
我得到:
Cannot read file http://some_bucket_name.s3.amazonaws.com/uploads/users/28/songs/88/test.mp3
该文件确实存在。如果我手动导航到该网址。该文件播放正常。
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', :disposition => '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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这适用于 Rails 3。在您的控制器中执行以下操作:
在您的情况下:
This works for Rails 3. In your controller do:
In your case:
尝试
send_data AWS::S3.new.buckets['music'].objects["path/to/your/file.mp3"].read,文件名:"some_file_name.mp3"
try
send_data AWS::S3.new.buckets['music'].objects["path/to/your/file.mp3"].read, filename: "some_file_name.mp3"