我想将数据从FTP A复制到FTPB。
我使用流而不是本地临时文件。
但是我无法从流中读取数据。

transformComponent.js

const Transform = require('stream').Transform;
const util = require('util');

const transformComponent = module.exports = function(options) {
    if (!(this instanceof transformComponent))
        return new Parser(options);
    Transform.call(this, options);
};

util.inherits(transformComponent, Transform);

transformComponent.prototype._transform = function(data, encoding, callback) {
    this.push(data);
    callback();
};

transformComponent.prototype._flush = function(callback) {
    callback();
};


ftpComponent.js

const Client = require('ftp');
var ftp = new Client();

var ftpComponent = function(host){
    this.host = host;
    this.port = 21;
    this.user = "";
    this.password = "";
};

function connectionProperties(){
    let properties ={
        host: this.host,
        port: this.port
    };
    if(this.user && this.user!== "")
        properties.user = this.user;
    if(this.password && this.password !== "")
        properties.password = this.password;

    return properties;
}

function download(filepath, target){
    ftp.on('ready', function () {
      ftp.get(filepath, function(err, stream) {
        if (err) throw err;
        stream.once('close', function() { ftp.end(); });
        stream.pipe(target);
      });
    });

    ftp.connect(this.connectionProperties());
}

function upload(source, filename){
    ftp.on('ready', function () {
        // Upload files to the server:
        ftp.put(source, filename, function(err) {
            if (err) throw err;
            ftp.end();
        });
    });

    ftp.connect(this.connectionProperties());
}

ftpComponent.prototype = {
    connectionProperties: connectionProperties,
    pull : download,
    push : upload
}

module.exports = ftpComponent;


我的用法:

const ftp = require('./node_component/ftpComponent.js');
const transform = require('./node_component/transformComponent.js');

var ftpSourceObject = new ftp('host A');
var ftpTargetObject = new ftp('host B');

ftpSourceObject.user = usernameA;
ftpSourceObject.password = passwordA;

ftpTargetObject.user = usernameB;
ftpTargetObject.password = passwordB;

var temp = new transform();
ftpTargetObject.push(temp, 'file-cp.txt');
ftpSourceObject.pull('file.txt', temp);


我可以将数据从FTP A写入流。
但是当从流中读取数据并放在FTP B上时。
它显示如下错误。

Error: Unable to parse PASV server response
    at Object.reentry [as cb] (/home/locadmin/fileshareservice/app/node_modules/ftp/lib/connection.js:857:19)
    at Parser.<anonymous> (/home/locadmin/fileshareservice/app/node_modules/ftp/lib/connection.js:117:20)
    at Parser.emit (events.js:182:13)
    at Parser._write (/home/locadmin/fileshareservice/app/node_modules/ftp/lib/parser.js:59:10)
    at doWrite (_stream_writable.js:410:12)
    at writeOrBuffer (_stream_writable.js:394:5)
    at Parser.Writable.write (_stream_writable.js:294:11)
    at Socket.ondata (/home/locadmin/fileshareservice/app/node_modules/ftp/lib/connection.js:273:20)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)


但是当我只跑一行

var temp = new transform();
ftpTargetObject.push(temp, 'file-cp.txt');
temp.write('123');
temp.end();
//ftpSourceObject.pull('file.txt', temp);


工作成功。
如何将流作为参数传递给函数?
对不起,我英语不好...

我的源代码参考源是https://bountify.co/node-js-script-for-copying-files-between-two-ftp-locations

最佳答案

我修理它。

transformComponent.js

const Transform = require('stream').Transform;
const util = require('util');

const transformComponent = module.exports = function(options) {
    if (!(this instanceof transformComponent))
        return new Parser(options);
    Transform.call(this, options);
};

util.inherits(transformComponent, Transform);

transformComponent.prototype._transform = function(data, encoding, callback) {
    this.push(data);
    callback();
};

transformComponent.prototype._flush = function(callback) {
    callback();
};


ftpComponent.js

const Client = require('ftp');

var ftpComponent = function(host){
    this.host = host;
    this.port = 21;
    this.user = "";
    this.password = "";
};

function connectionProperties(){
    let properties ={
        host: this.host,
        port: this.port
    };
    if(this.user && this.user!== "")
        properties.user = this.user;
    if(this.password && this.password !== "")
        properties.password = this.password;

    return properties;
}

function download(filepath, target){
    var ftp = new Client();
    ftp.on('ready', function () {
      ftp.get(filepath, function(err, stream) {
        if (err) throw err;
        stream.once('close', function() { ftp.end(); });
        stream.pipe(target);
      });
    });

    ftp.connect(this.connectionProperties());
}

function upload(source, filename){
    var ftp = new Client();
    ftp.on('ready', function () {
        // Upload files to the server:
        ftp.put(source, filename, function(err) {
            if (err) throw err;
            ftp.end();
        });
    });

    ftp.connect(this.connectionProperties());
}

ftpComponent.prototype = {
    connectionProperties: connectionProperties,
    pull : download,
    push : upload
}

module.exports = ftpComponent;


我的用法:

const ftp = require('./node_component/ftpComponent.js');
const transform = require('./node_component/transformComponent.js');

var ftpSourceObject = new ftp('host A');
var ftpTargetObject = new ftp('host B');

ftpSourceObject.user = usernameA;
ftpSourceObject.password = passwordA;

ftpTargetObject.user = usernameB;
ftpTargetObject.password = passwordB;

var temp = new transform();
ftpTargetObject.push(temp, 'file-cp.txt');
ftpSourceObject.pull('file.txt', temp);

10-01 13:09