gpt4 book ai didi

ruby-on-rails-3 - 允许用户从 S3 存储下载文件

转载 作者:行者123 更新时间:2023-12-03 15:08:49 25 4
gpt4 key购买 nike

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

这是我目前的设置

索引.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'

希望有人可以帮助我。

最佳答案

为了避免给您的应用程序带来额外的负载(在 Heroku 中节省 dyno 的时间),我宁愿做这样的事情:将此方法添加到您的模型中,并附上附件:

def download_url(style_name=:original)
creative.s3_bucket.objects[creative.s3_object(style_name).key].url_for(:read,
:secure => true,
     :expires => 24*3600,  # 24 hours
     :response_content_disposition => "attachment; filename='#{creative_file_name}'").to_s
end

然后在您的 View / Controller 中使用它,如下所示:
<%= link_to 'Download Creative', event.download_url, class: "btn btn-info" %>

关于ruby-on-rails-3 - 允许用户从 S3 存储下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12939903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com