Hi I wanted to upload CSV file from my S3 bucket to SFTP server.Can you please guide me how can I do this in NodeJS and what are the npm module which can be used to upload s3 bucket csv file to SFTP server.
Thanks
Get S3 bucket CSV file content :
var getS3UploadedCSVFileContent = function(next, results) {
var filePath = 'your_file_path';
var s3 = new AWS.S3();
var params = {
Bucket: 'your_bucket_name',
Key: filePath
}
s3.getObject(params, function(err, data) {
if (err) {
console.log("Error in getting CSV file from S3 bucket", err);
} else {
console.log("Content is", data);
var dataObject = data.Body.toString();
next(null, dataObject);
}
})}
Upload CSV file on SFTP Server :
var uploadCSVFileOnSftpServer = function(next, s3FileStreamContent) {
var filePath = 'your_file_path';
var Client = require('ssh2').Client;
var connSettings = {
host: 'your_server_host_name',
port: 22,
username: 'user_name',
password: 'password'
};
var conn = new Client();
conn.on('ready', function() {
conn.sftp(function(err, sftp) {
if (err) {
console.log("Errror in connection", err);
} else {
console.log("Connection established", sftp);
var options = Object.assign({}, {
encoding: 'utf-8'
}, true);
var stream = sftp.createWriteStream(filePath, options);
var data = stream.end(s3FileStreamContent);
stream.on('close', function() {
console.log("- file transferred succesfully");
conn.end();
next(null, true);
});
}
});
}).connect(connSettings);}