允许用户从S3存储下载文件文件、用户

2023-09-11 08:13:07 作者:思念抹不去的记忆

现在,我使用Amazon S3和回形针是让我的用户上载了与他们创建的事件相关联的图像。我的最终目标是,因为其他人可以查看本次活动,以便能够在图像上单击,并让它提示保存到自己的电脑。截至目前,点击链接将在浏览器窗口中打开图像。我宁愿把它要求他们下载吧。所有图像只保存到S3,而不是本地的。需要隐藏暴露S3网址,以及如果可能的话,或伪装它

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'

希望有人能帮助我。

Hoping someone can help me out.

推荐答案

为使这项工作,我只是增加了一个新的动作控制器,所以你的情况可能是:

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

编辑: 下载控制器动作正确的解决方案,可以在这里找到(我已经更新了code以上):Force一个链接下载一个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?