允许用户从 S3 存储下载文件 [英] Allowing User to Download File from S3 Storage

查看:41
本文介绍了允许用户从 S3 存储下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在使用 Amazon S3 和 Paperclip,它允许我的用户上传与他们正在创建的事件相关联的图像.我的最终目标是因为其他人可以查看此事件,以便能够单击图像并使其提示保存到他们的计算机.截至目前,单击该链接将在浏览器窗口中打开图像.我宁愿要求他们下载.所有图像仅保存在 S3 上,而非本地.如果可能,还需要隐藏暴露的 s3 url 或伪装它

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

这是我当前的设置

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'

希望有人能帮助我.

推荐答案

为了完成这项工作,我刚刚在控制器中添加了一个新操作,因此在您的情况下可能是:

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

下载控制器操作的正确解决方案可以在这里找到(我已经更新了上面的代码):强制链接下载 MP3 而不是播放它?

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?

这篇关于允许用户从 S3 存储下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆