【问题标题】:FTP client on Node.js for Windows Server适用于 Windows Server 的 Node.js 上的 FTP 客户端
【发布时间】:2020-09-29 16:11:22
【问题描述】:

有货

  • 安装了 Node.js 的 Linux (Debian) 发行版第三方
  • 第三方 Windows 服务器

任务

需要使用 Node.js 平台建立到 Windows Server 的 FTP 连接,剪切一个特定文件夹(连同其中的文件夹和文件)并将其粘贴到运行 Linux 的服务器上的特定目录中。

问题

是否有可能实现所描述的任务?如果是,使用哪些 NPM 包和/或哪些程序代码适合解决问题?

【问题讨论】:

    标签: node.js linux windows-server-2008 windows-server-2012


    【解决方案1】:

    你可以使用来自 npm 的 ssh2-sftp-client

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

    【讨论】:

    • 确保您使用正确的用户名、密码并且您的 IP 地址被列入白名单以访问 sftp 位置(如果它是安全的)
    【解决方案2】:
    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()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 2022-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多