简体   繁体   English

make rails如何从as s3块中逐块获取数据并将其作为pdf下载文件发送到浏览器?

[英]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. 我对如何从aws s3获取文件而不将其写为文件但可能作为自动删除的临时文件感到困惑。 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? 如何实际从aws(pdf文件)流式传输数据并将其作为可下载的pdf文件发送到浏览器? 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. 此方法还具有重试网络错误的好处,因此,与禁用错误重试功能的分块方法相比,它更具弹性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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