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