简体   繁体   English

使用Bluebird承诺节点SSH2 SFTP

[英]Promisifying Node SSH2 SFTP with Bluebird

I am using https://github.com/mscdex/ssh2 for my FTP requirements. 我将https://github.com/mscdex/ssh2用于我的FTP要求。 I tried below mentioned code: 我试过下面提到的代码:

'use strict';

global.Promise = require("bluebird");
global._ = require('lodash');

var Client = Promise.promisifyAll(require('ssh2').Client);
Promise.promisifyAll(Client.prototype);

var clientObject, conn;

function getClient() {
    return new Promise((resolve, reject) => {
        if (clientObject) resolve(clientObject);

        let authObj = {
            host: 'abc',
            port: 22,
            username: 'name',
            password: 'password'
        };

        conn = new Client();
        conn.connect(authObj);

        conn.on('ready', () => {
            conn.sftp((err, sftp) => {
                if (err) reject(err);
                clientObject = sftp;
                resolve(clientObject)
            });
        });

        conn.on('error', (err) => { reject(err) });
    });
}


function getList(){
    getClient().then((cObj)=>{
        cObj.readdir('.').then((list)=>{
            _.each(list, (file)=>{console.log(file.filename)});
        });
    });
}


getList();

Above is given errors as: 上面给出的错误为:

Unhandled rejection TypeError: cObj.readdir(...).then is not a function
.
.
.
/home/nk/test/node_modules/ssh2-streams/lib/sftp.js:1583
            cb(undefined, entries);
            ^

TypeError: cb is not a function
    at /home/nk/test/node_modules/ssh2-streams/lib/sftp.js:1583:13

From above error; 从上面的错误; it looks like it is pointing to this line https://github.com/mscdex/ssh2-streams/blob/master/lib/sftp.js#L1583 它似乎指向此行https://github.com/mscdex/ssh2-streams/blob/master/lib/sftp.js#L1583

Is it possible to achieve ? 有可能实现吗?

I have created a ssh2-promise npm package for catering the same. 我已经创建了一个ssh2-promise npm包来满足相同的需求。 I have promisified SSH2 and SFTP item. 我已答应SSH2和SFTP项目。 Hopefully, it will help 希望它将有所帮助

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

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