简体   繁体   English

Node.js 上的 FTP 客户端,用于 Windows 服务器

[英]FTP client on Node.js for Windows Server

In stock有存货

  • Linux (Debian) distribution with Node.js installed Third-party Linux (Debian) 发行版,安装了 Node.js 第三方
  • Third-party Windows Server第三方Windows服务器

Task任务

It is necessary to establish an FTP connection to Windows Server using the Node.js platform, cut out a specific folder (along with the folders and files located in it) and paste it into a specific directory on a server running Linux. It is necessary to establish an FTP connection to Windows Server using the Node.js platform, cut out a specific folder (along with the folders and files located in it) and paste it into a specific directory on a server running Linux.

Question问题

Is it possible to implement the described task?是否有可能实现所描述的任务? If yes, what NPM packages to use and / or what program code is suitable for solving the problem?如果是,使用什么 NPM 包和/或什么程序代码适合解决问题?

You can use ssh2-sftp-client package from npm您可以使用来自 npm的 ssh2-sftp-client package

const Client = require('ssh2-sftp-client');

const config = {
  host: 'example.com',
  port: 22,
  username: 'red-don',
  password: 'my-secret'
};

let sftp = new Client;

sftp.connect(config)
  .then(() => {
    return sftp.list('/path/to/remote/dir');
  })
  .then(async data => {
    console.log(data);
    for(let file of data) {
        //you can check if file is directory or file
        if(file.type == 'd') {
           //recursively read files
        }else {
            //download file
            const remoteFilename = '/path/to/remote/dir/' + data.name;
            const localFilename = '/path/to/local/files/' + data.name;
            sftp.get(remoteFilename).then((stream) => {
                stream.pipe(fs.createWriteStream(localFilename));
            });`enter code here`
        }
    } 
    //delete directory recursively after all files are download 
    return client.rmdir(/path/to/remote/dir, true); 
  })
  .then(() => {
    sftp.end();
  })
  .catch(err => {
    console.error(err.message);
  });
const ftp = require('basic-ftp');

ftp_connect()
 
async function ftp_connect() {
    const client = new ftp.Client();
    client.ftp.verbose = true;
    try {
        await client.access({
            host: 'host',
            port: '21',
            user: 'domain\\username',
            password: 'password',
            secure: false
        })
        console.log(await client.list())
    }
    catch(err) {
        console.log(err)
    }
    client.close()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM