问题
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.
- 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