确定节点js中读取流的长度 [英] Determining the length of a read stream in node js
问题描述
我想知道是否有一种简单的方法来确定节点js中readStream的长度.我正在写一个工人将PDF上传到S3,但出现错误:无法确定[对象PDFDocument]的长度". pdf是可读的流(我可以将其通过管道传输到当前内存中的文件中,并查看生成的pdf).
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).
我正在考虑做的一些事情: 1.将整个pdf放入内存(例如缓冲区),然后在该缓冲区上调用fstat以确定大小.
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.
- 创建一些在通过pdf将其通过管道动态找到尺寸时使用的方法.
还是有一种更简单的方法?开放所有建议,在此先感谢!
Or is there an easier way? Open to all suggestions, thanks in advance!
这是对S3的呼叫.可读的pdf流称为doc:
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);
})
推荐答案
S3还有另一种称为上载"的方法,您可以在不需要知道长度的地方使用它.
S3 has another method called 'upload' that you can use where you don't need to know the length.
这篇关于确定节点js中读取流的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!