Read file from aws s3 bucket using node fs

后端 未结 11 784
逝去的感伤
逝去的感伤 2020-12-07 21:37

I am attempting to read a file that is in a aws s3 bucket using

fs.readFile(file, function (err, contents) {
  var myLines = contents.Body.toString().split(         


        
11条回答
  •  眼角桃花
    2020-12-07 22:35

    I had exactly the same issue when downloading from S3 very large files.

    The example solution from AWS docs just does not work:

    var file = fs.createWriteStream(options.filePath);
            file.on('close', function(){
                if(self.logger) self.logger.info("S3Dataset file download saved to %s", options.filePath );
                return callback(null,done);
            });
            s3.getObject({ Key:  documentKey }).createReadStream().on('error', function(err) {
                if(self.logger) self.logger.error("S3Dataset download error key:%s error:%@", options.fileName, error);
                return callback(error);
            }).pipe(file);
    

    While this solution will work:

        var file = fs.createWriteStream(options.filePath);
        s3.getObject({ Bucket: this._options.s3.Bucket, Key: documentKey })
        .on('error', function(err) {
            if(self.logger) self.logger.error("S3Dataset download error key:%s error:%@", options.fileName, error);
            return callback(error);
        })
        .on('httpData', function(chunk) { file.write(chunk); })
        .on('httpDone', function() { 
            file.end(); 
            if(self.logger) self.logger.info("S3Dataset file download saved to %s", options.filePath );
            return callback(null,done);
        })
        .send();
    

    The createReadStream attempt just does not fire the end, close or error callback for some reason. See here about this.

    I'm using that solution also for writing down archives to gzip, since the first one (AWS example) does not work in this case either:

            var gunzip = zlib.createGunzip();
            var file = fs.createWriteStream( options.filePath );
    
            s3.getObject({ Bucket: this._options.s3.Bucket, Key: documentKey })
            .on('error', function (error) {
                if(self.logger) self.logger.error("%@",error);
                return callback(error);
            })
            .on('httpData', function (chunk) {
                file.write(chunk);
            })
            .on('httpDone', function () {
    
                file.end();
    
                if(self.logger) self.logger.info("downloadArchive downloaded %s", options.filePath);
    
                fs.createReadStream( options.filePath )
                .on('error', (error) => {
                    return callback(error);
                })
                .on('end', () => {
                    if(self.logger) self.logger.info("downloadArchive unarchived %s", options.fileDest);
                    return callback(null, options.fileDest);
                })
                .pipe(gunzip)
                .pipe(fs.createWriteStream(options.fileDest))
            })
            .send();
    

提交回复
热议问题