Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

527
Views
ssh2 nodejs | upload file to sftp | Error: Permission denied at 101

When trying to upload a file to sftp server permission denied error appears. The same operation works if file is transferred via FilezIlla.

const UploadFiletoFTP = () => {
      let Client = require('ssh2').Client;
      var connSettings = {
        host: 'abc.com',
        port: 22,
        username: 'user',
        password: 'pass',
      };

  var conn = new Client();
  conn
    .on('ready', function () {
      conn.sftp(function (err, sftp) {
        try {
          if (err) {
            console.log(err);
            throw 'error ' + err;
          }
          console.log('connected');
          var fs = require('fs'); // Use node filesystem
          var readStream = fs.createReadStream(
            require('path').join(
              __dirname +
                '/audio/test_data_25_05_2022_09_58_00.zip'
            )
          );

          sftp.readdir(
            'speech/non-english',
            function (err, list) {
              if (err) throw err;
              // List the directory in the console
              console.dir(list);
              // Do not forget to close the connection, otherwise you'll get troubles
              conn.end();
            }
          );

          var writeStream = sftp.createWriteStream('SpeechIQ', {
            flags: 'a', // w - write and a - append
            encoding: null, // use null for binary files
            mode: 0o666, // mode to use for created file (rwx)
          });

          writeStream.on('close', function () {
            console.log('- file transferred succesfully');
          });

          writeStream.on('end', function () {
            console.log('sftp connection closed');
            conn.end();
          });

          readStream.pipe(writeStream);
        } catch (err) {
          console.error(err);
        }
      });
    })
    .connect(connSettings);
};

UploadFiletoFTP();

When the above code is run below error appears:

events.js:377
      throw er; // Unhandled 'error' event
      ^

Error: Permission denied
    at 101
Emitted 'error' event on Client instance at:
.
.
.
.
  code: 3
}

Please advise if I am missing something.

Below snippet lists the files in the directory but the writestream is not working.

sftp.readdir(
            'speech/non-english',
            function (err, list) {
              if (err) throw err;
              // List the directory in the console
              console.dir(list);
              // Do not forget to close the connection, otherwise you'll get troubles
              conn.end();
            }
          );
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I had to give the filename to which the data has to be uploaded

var writeStream = sftp.createWriteStream('SpeechIQ/filename.zip', {
            flags: 'a', // w - write and a - append
            encoding: null, // use null for binary files
            mode: 0o666, // mode to use for created file (rwx)
          });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!