node.jssftpssh2-sftp-client

Failure code 4 while uploading file to SFTP server using Node.js


I have written code to establish a SFTP connection and transfer files to the SFTP server using Node.js sftp.put command. I'm getting the following error while transferring the file. I can establish the connection successfully. But I cannot read/write files to the server. I have attached the code below

Code

let sftp = new client();

  let filename = "sound.mp3";
  const filePath = path.join(__dirname, '../audio', filename)
  
   const putConfig = {
    flags: 'w', // w - write and a - append
    encoding: null, // use null for binary files
    mode: 0o666, // mode to use for created file (rwx)
    autoClose: true // automatically close the write stream when finished
  };
  
  sftp.connect({
      host: 'host',
      port: '22',
      username: 'xxxx',
      password: 'xxx'
    }).then(() => {
      return sftp.put(filePath, '/', putConfig)
    }).then(data => {
      console.log(data, 'the data info');
    }).catch(err => {
      console.log(err, 'catch error');
    });

Error

Error: put->put: Failure /data
    at fmtError (D:\project\node_modules\ssh2-sftp-client\src\utils.js:53:18)
    at SftpClient.put (D:\project\node_modules\ssh2-sftp-client\src\index.js:684:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  code: 4,
  custom: true
}
D:\project\node_modules\ssh2\lib\protocol\crypto\poly1305.js:20
function J(a){if(b.onAbort)b.onAbort(a);L(a);O=!0;a=new WebAssembly.RuntimeError("abort("+a+"). Build with -s ASSERTIONS=1 for more info.");r(a);throw a;}var V="data:application/octet-stream;base64,",W="data:application/octet-stream;base64,AGFzbQEAAAABIAZgAX8Bf2ADf39/AGABfwBgAABgAAF/YAZ/f39/f38AAgcBAWEBYQAAAwsKAAEDAQAAAgQFAgQFAXABAQEFBwEBgAKAgAIGCQF/AUGAjMACCwclCQFiAgABYwADAWQACQFlAAgBZgAHAWcABgFoAAUBaQAKAWoBAAqGTQpPAQJ/QYAIKAIAIgEgAEEDakF8cSICaiEAAkAgAkEAIAAgAU0bDQAgAD8AQRB0SwRAIAAQAEUNAQtBgAggADYCACABDwtBhAhBMDYCAEF/C4wFAg5+Cn8gACgCJCEUIAAoAiAhFSAAKAIcIREgACgCGCESIAAoAhQhEyACQRBPBEAgAC0ATEVBGHQhFyAAKAIEIhZBBWytIQ8gACgCCCIYQQVsrSENIAAoAgwiGUEFbK0hCyAAKAIQIhpBBWytIQkgADUCACEIIBqtIRAgGa0hDiAYrSEMIBatIQoDQCASIAEtAAMiEiABLQAEQQh0ciABLQAFQRB0ciABLQAGIhZBGHRyQQJ2Qf///x9xaq0iAyAOfiABLwAAIAEtAAJBEHRyIBNqIBJBGHRBgICAGHFqrSIEIBB+fCARIAEtAAdBCHQgFnIgAS0ACEEQdHIgAS0ACSIRQRh0ckEEdkH///8fcWqtIgUgDH58IAEtAApBCHQgEXIgAS0AC0EQdHIgAS0ADEEYdHJBBnY

RuntimeError: abort(Error: put->put: Failure /data). Build with -s ASSERTIONS=1 for more info.
    at process.J (D:\project\node_modules\ssh2\lib\protocol\crypto\poly1305.js:20:53)
    at process.emit (events.js:210:5)
    at process.EventEmitter.emit (domain.js:475:20)
    at processPromiseRejections (internal/process/promises.js:201:33)
    at processTicksAndRejections (internal/process/task_queues.js:94:32)

Solution

  • The second argument of SftpClient.put is a path to the target remote file, not only path to the target remote folder.

    So it should be like:

    return sftp.put(filePath, '/' + filename, putConfig)