Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

518
Visualizações
NodeJS ssh2-sftp-client | connecting to SFTP using public key and a passphrase

I'm having a problem connecting to SFTP server using public key and a passphrase. I've tried the following code but it is infinitely calling the callback function.

I appreciate any input. Thank you.


let sftpClient = require('ssh2-sftp-client');

let sftp = new sftpClient();

let conf = {
    host: 'host',
    port: 'port',
    username: 'username',
    keepaliveInterval: 1000
};

conf.authHandler = function (methodsLeft, partialSuccess, callback) {
    console.log('authhandler invoked')
    callback({
        type: 'publickey',
        username: 'username',
        passphrase: 'password',
        key: fs.readFileSync('./id_rsa.pub', 'utf8')
    });
}

sftp.connect(conf).then(() => {
    console.log('connected')
    // upload process here

}).then(data => {

    sftp.end()
}).catch(err => {
    console.log(err, 'catch error');
    sftp.end()
});

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is my working solution. You can try on your server

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

    async upload(file) {
      const sftp = new sftpClient();
      const sftpSSHKey = fs.readFileSync(keyPath);

      try {
          await sftp.connect({
            host: 'somehost',
            port: 'port',
            username: 'username',
            privateKey: sftpSSHKey,
            passphrase: 'passphrase for an encrypted private key',
          });
          console.log('Successfully connected to sftp');
        } catch (error: any) {
          console.log(error);
        }

        try {
          const res = await client.put(Buffer.from(file), '/folder/fileName', {
            writeStreamOptions: {
              flags: 'w',
              encoding: null, // use null for binary files
            },
          });
           console.log('File uploaded successfully');
        } catch (error) {
          console.log(error);
        } finally {
          await client.end(); // don't forget to close connection
        }
    }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda