Determining the length of a read stream in node js

久未见 提交于 2019-12-10 21:45:15

问题


I am wondering if there is an easy way to determine the length of a readStream in node js. I am writing a worker to upload PDFs to S3 and it is puking with the error: 'Cannot determine length of [object PDFDocument]'. The pdf is a readable stream (I can pipe it to a file in memory currently and view the generated pdf).

Some stuff I was thinking about doing: 1. Piping the entire pdf to memory (such as to a buffer) and then call fstat on that buffer to determine the size.

  1. Creating some method that dynamically found the size as I piped the pdf through it.

Or is there an easier way? Open to all suggestions, thanks in advance!

Here is the call to S3. The readable pdf stream is called doc:

return Q.ninvoke(s3, 'putObject', {Bucket: 'REDACTED', Key: obfuscatedFN, Body: doc, ContentType: 'application/pdf'})
     .then(function() {
           console.log("Successfully uploaded data to myBucket/myKey");
           return Q(true);
     })
     .fail(function(err) {
           console.log(err);
     })

回答1:


S3 has another method called 'upload' that you can use where you don't need to know the length.



来源:https://stackoverflow.com/questions/30227352/determining-the-length-of-a-read-stream-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!