Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
Rails download of AWS S3 file using presigned_post - fatal exception

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:

fatal at /user/doc_uploads/download exception reentered

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

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:

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

view:

<% @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 %>

Thanks in advance!

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

If you are able to get url properly in your code, So you can also able to read image from that url.

Try this:

def get
  @doc_download = @user.doc_uploads.find_by_id(params[:id])
  if @doc_download
    data = open(@doc_download.file_url) 
    send_data data.read, filename: 'filename', stream: 'true', buffer_size: '4096'
  else
    flash[:error]="Something went wrong."
    redirect_to user_dashboard_path
  end
end
over 4 years ago · Santiago Trujillo Denunciar

0

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
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda