如何从 Rails 应用程序提供 S3 文件?

发布于 2024-12-08 20:37:59 字数 887 浏览 5 评论 0原文

我试图允许用户通过左键单击链接来下载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清旖 2024-12-15 20:37:59

这适用于 Rails 3。在您的控制器中执行以下操作:

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
  }
}

在您的情况下:

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

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'
忱杏 2024-12-15 20:37:59

尝试
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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文