在Cloud Functions中使用ssh2 sftp发送文件

时间:2019-03-20 07:06:31

标签: node.js google-cloud-storage google-cloud-functions ssh2-sftp

我想在云功能中使用SFTP发送文件。该文件将被命名为带有ASCII扩展名的今天的日期,并存储在Google Cloud Storage中。因此,为了在云函数中使用文件,我使用了/ tmp。 我已经使用Cyber​​duck在Mac中建立服务器。但是它总是说ETimeout。我无法使用SFTP发送文件

const file = bucketname.file(new Date().toISOString().slice(0,10)+".ascii"); // It create the file in the bucket in ascii format and save as today date format.
const uploadStream = file.createWriteStream();
uploadStream.on('error', function(err) {
console.log("write err",err);
});
finalnachaformat.forEach(function(v) { // It takes the each value in finalnachaformat in every loop.
    console.log("v",v)
    uploadStream.write(v+ '\n'); // and write data into file.
    });
 console.log("successfully uploaded")
uploadStream.end();
file.download({
  destination: tempFilePath
}, function(err) {
console.log(err);
});
fs.readFile("/tmp/myText.ascii", "utf-8", function(err, buf) {
if (err)
console.log(err);
else {
readData = buf.toString();
}
});
console.log(readData);
let sftp = new Client();
 sftp.connect({
     host: '**',
     port: '22',
     readyTimeout: 99999,
     debug: console.log,
     username: '**',
     password: '**'
 }).then(() => {
     return sftp.put(tempFilePath,'/Users/DG/Downloads/huh.ascii');
 }).then((data) => {
     console.log(data, 'the data info');
 }).catch((err) => {
     console.log(err, 'catch error');
 });
res.send("ok");
}
});
exports.downloadFile = functions.https.onRequest((req,res) =>{
let Client = require('ssh2-sftp-client');
const fs  = require("fs");

///const file = bucket.file("normal.txt");
fs.writeFile("/tmp/myFile.txt", file, function(err, data) {
if (err)
console.log(err);
else
console.log("Write to file success");
});
res.send(200);
});

有人能找出错误吗?

0 个答案:

没有答案