本文介绍了使用presigned_post的Rails下载AWS S3文件-致命异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Rails应用程序设置为使用本教程,效果很好.现在,我正在使用.当我尝试触发下载时,它挂起,然后最终返回以下错误:

I set up my Rails app to upload files directly to AWS S3 using this tutorial and it works great. Now I'm working on the direct download piece using the same approach in this SO post. When I try and trigger the download it hangs and then finally returns the following error:

下面的代码中的puts显示了服务器上似乎是无限循环的情况.我只是对该代码应该如何工作才能弄清楚这一点还不了解.请帮忙!

The puts in the code below show what appears to be an infinite loop on the server. I simply don't know enough about how this code is supposed to work to figure this out. Please help!

doc_uploads_controller.rb

doc_uploads_controller.rb

def get
    @doc_download = @user.doc_uploads.find_by_id(params[:id])
    if @doc_download
      key = @doc_download.file_url.split('amazonaws.com/')[1]
      puts key
      puts S3_BUCKET_NAME
      bucketlink = S3_BUCKET_NAME.object(key).presigned_url(‌​:get, expires_in: 3600)
      puts bucketlink
      redirect_to bucketlink
    else
      flash[:error]="Something went wrong."
      redirect_to user_dashboard_path
    end
  end

aws.rb:

Aws.config.update({
  region: 'us-east-1',
  credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']),
})

S3_BUCKET_NAME = Aws::S3::Resource.new.bucket(ENV['S3_BUCKET_NAME'])

routes.rb:

routes.rb:

get 'user/doc_uploads/download', to: 'doc_uploads#get'

视图:

<% @doc_uploads.each do |doc_upload| %>
...
      <%= link_to user_doc_uploads_download_path(id: doc_upload.id), target: '_blank' do %>
        <p class="glyphicon glyphicon-download-alt"></p>
      <% end %>
...
<% end %>

提前谢谢!

推荐答案

找到了最近的帖子,并使其解决方案正常运行,例如:

Found a recent post here and got their solution working, as:

presigner = Aws::S3::Presigner.new
url = presigner.presigned_url(:get_object, bucket: ENV['S3_BUCKET_NAME'], key: key, expires_in: 3600, response_content_disposition: "attachment").to_s
redirect_to url

这篇关于使用presigned_post的Rails下载AWS S3文件-致命异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:11