简体   繁体   中英

How make rails get data from aws s3 chunk by chunk and send it ti browser as pdf download file?

I have some confusion with how to get file from aws s3 without write it as file but maybe as a tempfile that is deleted automatically. so my friend tell me to buffer stream the data chunk by chunk and send it to browser as downloadable file.

So here it is my code for downloading the file

 def download(key)
   File.open('filename', 'wb') do |file|
    s3.get_object(bucket: 'bucket-test', key:key) do |chunk|
     send_data(chunk,:type => application/pdf, :disposition => 'inline')
    end
   end
 end

it comes with and error about seahorse cannot be convert to string. and i dont actually understand that.

How to actually do stream the data from aws (pdf file) and send it to browser as downloadable pdf file? is my code not like what i inteded for?

thank you kindly

Just retrieve the whole file into memory and then send it out:

response = s3.get_object(bucket: 'bucket-test', key: key)
send_data(response.body.read, type: application/pdf, disposition: 'inline')

This method also has the benefit that it will retry network errors, so it's more resilient that the chunked method which disable retries on error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM