Node.js - copy file from FTP A to FTP B by stream












1















I want to copy data from FTP A to FTP B.
I use stream instead of the local temp file.
But i cannot read the data from stream.



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;


My usage :



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);


I can write data into stream from FTP A.
But when reading data from stream and put on FTP B.
It shows the error like below.



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)


But when i run just a single line



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


It work successful.
How can I pass the stream as parameter to function??
Sorry for my poor English...



My source code reference source is https://bountify.co/node-js-script-for-copying-files-between-two-ftp-locations.










share|improve this question





























    1















    I want to copy data from FTP A to FTP B.
    I use stream instead of the local temp file.
    But i cannot read the data from stream.



    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;


    My usage :



    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);


    I can write data into stream from FTP A.
    But when reading data from stream and put on FTP B.
    It shows the error like below.



    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)


    But when i run just a single line



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


    It work successful.
    How can I pass the stream as parameter to function??
    Sorry for my poor English...



    My source code reference source is https://bountify.co/node-js-script-for-copying-files-between-two-ftp-locations.










    share|improve this question



























      1












      1








      1








      I want to copy data from FTP A to FTP B.
      I use stream instead of the local temp file.
      But i cannot read the data from stream.



      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;


      My usage :



      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);


      I can write data into stream from FTP A.
      But when reading data from stream and put on FTP B.
      It shows the error like below.



      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)


      But when i run just a single line



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


      It work successful.
      How can I pass the stream as parameter to function??
      Sorry for my poor English...



      My source code reference source is https://bountify.co/node-js-script-for-copying-files-between-two-ftp-locations.










      share|improve this question
















      I want to copy data from FTP A to FTP B.
      I use stream instead of the local temp file.
      But i cannot read the data from stream.



      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;


      My usage :



      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);


      I can write data into stream from FTP A.
      But when reading data from stream and put on FTP B.
      It shows the error like below.



      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)


      But when i run just a single line



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


      It work successful.
      How can I pass the stream as parameter to function??
      Sorry for my poor English...



      My source code reference source is https://bountify.co/node-js-script-for-copying-files-between-two-ftp-locations.







      node.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 2:02







      Yucheng.lin

















      asked Nov 22 '18 at 13:08









      Yucheng.linYucheng.lin

      164




      164
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I fix it.



          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;


          My usage :



          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);





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431740%2fnode-js-copy-file-from-ftp-a-to-ftp-b-by-stream%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I fix it.



            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;


            My usage :



            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);





            share|improve this answer




























              1














              I fix it.



              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;


              My usage :



              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);





              share|improve this answer


























                1












                1








                1







                I fix it.



                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;


                My usage :



                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);





                share|improve this answer













                I fix it.



                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;


                My usage :



                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);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 2:07









                Yucheng.linYucheng.lin

                164




                164
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431740%2fnode-js-copy-file-from-ftp-a-to-ftp-b-by-stream%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini