Отправьте файл, используя ssh2 sftp в облачных функциях - PullRequest
0 голосов
/ 20 марта 2019

Я хочу отправить файл с использованием SFTP в облачные функции.Файл будет назван как сегодняшняя дата с расширением ascii и был сохранен в Google Cloud Storage.Поэтому, чтобы использовать файлы в облачных функциях, я использовал / tmp.Я использовал Cyberduck, чтобы установить сервер в 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);
});

Может кто-нибудь разобраться с ошибкой?

...