NodeJS:从sftp服务器读取csv文件并将其转换为JSON

时间:2019-03-18 13:10:05

标签: node.js sftp ssh2-sftp

我正在使用ssh2-sftp-client读取CSV文件并创建ReadableStream。然后,我尝试在库csvtojson的帮助下将此可读流转换为Json。但是,我总是收到以下错误:

  

TypeError:readStream.pipe不是函数

到目前为止,这是我的代码:

let Client = require('ssh2-sftp-client');
let sftp = new Client();
var csv = require("csvtojson");

sftp.connect({
    host: 'HOST',
    port: 'PORT',
    username: 'USERNAME',
    password: 'PASSWORD'
}).then(() => {
    return sftp.get('/home/user/etc/testfile.csv');
}).then((data) => {
    csv()
        .fromStream(data)
        .subscribe(function (jsonObj) { //single json object will be emitted for each csv line
            // parse each json asynchronousely
            return new Promise(function (resolve, reject) {
                resolve()
                console.log(jsonObj);
            })
        })

}).catch((err) => {
    console.log(err, 'catch error');
});

有人知道我是否在正确地使用两个库,或者我尝试实现的目标是否不可能?

1 个答案:

答案 0 :(得分:1)

由于sftp.get返回了Buffer,而您正试图将其传递给期望Stream的函数,因此您收到错误消息。

将代码更改为此

let Client = require('ssh2-sftp-client');
let sftp = new Client();
var csv = require("csvtojson");

sftp.connect({
  host: 'HOST',
  port: 'PORT',
  username: 'USERNAME',
  password: 'PASSWORD'
}).then(() => {
  return sftp.get('/home/user/etc/testfile.csv');
}).then((data) => {
  csv()
    .fromString(data.toString()) // changed this from  .fromStream(data)
    .subscribe(function(jsonObj) { //single json object will be emitted for each csv line
      // parse each json asynchronousely
      return new Promise(function(resolve, reject) {
        resolve()
        console.log(jsonObj);
      })
    })
}).catch((err) => {
  console.log(err, 'catch error');
});