将S3存储桶CSV文件上传到NodeJS中的SFTP服务器

时间:2017-12-20 12:01:10

标签: csv amazon-s3 sftp

您好我想将我的S3存储桶中的CSV文件上传到SFTP服务器。您能否指导我如何在NodeJS中执行此操作以及可用于将s3存储桶csv文件上传到SFTP服务器的npm模块是什么。

由于

1 个答案:

答案 0 :(得分:1)

获取S3存储桶CSV文件内容

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

在SFTP服务器上上传CSV文件

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