npm / fstream

Advanced FS Streaming for Node
ISC License
208 stars 43 forks source link

fstream is not a stream.Readable #40

Open russellyoung opened 9 years ago

russellyoung commented 9 years ago

I am trying to set up a pipeline getting a tgz file from S3, unzipping it, untarring it, and uploading the results back to S3. This works fine until the upload, when I get an error:

/Users/russell/lambda/gzip/node_modules/aws-sdk/lib/s3/managed_upload.js:346
    var buf = self.body.read(self.partSize - self.partBuffer.length) ||
                        ^
TypeError: undefined is not a function
    at ManagedUpload.fillStream (/Users/russell/lambda/gzip/node_modules/aws-sdk/lib/s3/managed_upload.js:346:25)
    at Entry.<anonymous> (/Users/russell/lambda/gzip/node_modules/aws-sdk/lib/s3/managed_upload.js:167:28)
    at Entry.emit (events.js:104:17)
    at Entry._read (/Users/russell/lambda/gzip/node_modules/tar/lib/entry.js:123:12)
    at Entry.end (/Users/russell/lambda/gzip/node_modules/tar/lib/entry.js:82:8)
    at Parse._process (/Users/russell/lambda/gzip/node_modules/tar/lib/parse.js:107:13)
    at BlockStream.<anonymous> (/Users/russell/lambda/gzip/node_modules/tar/lib/parse.js:47:8)
    at BlockStream.emit (events.js:107:17)
    at BlockStream._emitChunk (/Users/russell/lambda/gzip/node_modules/tar/node_modules/block-stream/block-stream.js:145:10)
    at BlockStream.write (/Users/russell/lambda/gzip/node_modules/tar/node_modules/block-stream/block-stream.js:45:10)

On investigation, it turns out that self.body in the above error message is a readable fstream, but it has no read() method. Following is the code used to demonstrate. Setting the conditional to 0 writes the untarred files to disk, which works fine. Setting it to 1 uploads the files to S3 which breaks. I put console logs into the code in managed_upload.js to confirm what was happening.

var aws = require('aws-sdk');
var s3 = new aws.S3({apiVersion: '2006-03-01'});
var zlib = require('zlib');
var tar = require('tar');
var fstream = require('fstream');
var stream = require('stream');
var fs = require('fs');

var file = fs.createReadStream('gunzip.tar.gz');
unzipped =    file.pipe(zlib.Unzip());
untarred =     unzipped.pipe(tar.Parse());
untarred.on('entry', function(entry) {
    var filename = entry.path;
    console.log('got ' + entry.type + ' ' + filename);
    if (entry.type == 'File') {
        if (1) {
            s3.upload({Bucket: 'my_bucket', Key: 'gunzip-test/' + filename, Body: entry}, {},
                      function(err, data) {
                          if (err) 
                              console.log('ERROR!');
                          else
                              console.log('OK');
                      });
        }
        else {
            entry.pipe(fstream.Writer({ 'path': '/tmp/mytest/' + filename })); /* Give the output file name */
        }
    }
});