gpt4 book ai didi

node.js - 错误回调不用于node.js lambda中的ssh2-sftp-client的链接then()

转载 作者:太空宇宙 更新时间:2023-11-04 01:56:34 33 4
gpt4 key购买 nike

我对 Node 和 lambda 非常陌生,所以我可能犯了一些愚蠢的错误。我创建了一个 node.js aws lambda 函数,它从 s3 事件中获取文件。如果文件是 gzip,它会解压缩,将其上传到 sftp 服务器,然后创建 sig 文件并将其上传到同一 sftp 服务器。当一切顺利时它可以工作,但它似乎无法正确触发错误。

sftp 命令使用 then 进行链接,因此我预计任何错误都会导致后续 then 失败。例如,如果我关闭 sftp 服务器,sftp 客户端将生成超时错误,但 lambda 永远不会看到回调错误,只会看到成功。日志确实向控制台显示了错误输出,但它似乎在执行其余 .then() 项之后使用了成功回调。连接是否未正确记录为 promise ?

示例日志:

...
Starting SFTP
Connected to sftp, starting sftp put for lastsub2.dat file.
{ Error: Timed out while waiting for handshake
at Timeout._onTimeout (/var/task/node_modules/ssh2/lib/client.js:687:19)
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5) level: 'client-timeout' } 'Error occured during sftp relay.'
END

示例代码:

console.log('Loading function');
const aws = require('aws-sdk');
const s3 = new aws.S3({
apiVersion: '2006-03-01'
});
const zlib = require('zlib');
const fs = require("fs");
const connSettings = {
host: 'xxx',
port: '22',
username: 'xxx',
password: 'xxx'
};

exports.handler = function (event, context, callback) {
console.log('Received event:', JSON.stringify(event, null, 2));
console.log('Bucket Name: ' + event.Records[0].s3.bucket.name);
console.log('Object Key: ' + decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')));
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
const params = {
Bucket: bucket,
Key: key,
};

s3.getObject(params, (err, data) => {
if (err) {
console.log(err);
const message = 'Error getting object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.';
console.log(message);
callback(message);
} else {
if (data.ContentType == 'application/x-gzip') {
console.log('CONTENT TYPE is application/x-gzip');
var dataStream = s3.getObject(params).createReadStream().pipe(zlib.Unzip());
console.log('Created unzip datastream');
console.log('Starting SFTP');
let Client = require('ssh2-sftp-client');
let sftp = new Client();
sftp.connect(connSettings)
.then(console.log('Connected to sftp, starting sftp put for ' + key.replace('.gz', '.dat') + ' file.'))
.then(() => {
console.log('Finished sftp put for ' + key.replace('.gz', '.dat') + ' file.');
return sftp.put(dataStream, key.replace('.gz', '.dat'), true, 'utf-8');
}).then(() => {
var sigFileName = key.replace('.gz', '.sig');
var sigStream = fs.createWriteStream('/tmp/' + sigFileName);
sigStream.end();
console.log('Created ' + sigFileName + ' sig file.');
var readStream = fs.createReadStream('/tmp/' + sigFileName);
console.log('Uploaded ' + sigFileName + ' sig file.');
return sftp.put(readStream, sigFileName, true, 'utf-8');
}).then(() => {
console.log('Ended sftp connection.');
return sftp.end();
})
.then(callback(null, 'Success'))
.catch ((err) => {
console.log(err, 'Error occured during sftp relay.');
callback('Error', err);
});
} else {
callback(null, 'Uploaded file not in gzip format, will not be processed.');
}
}
});
};

最佳答案

您遇到的问题是因为您没有从 then() 返回任何内容。结果是每个 then() 都会立即执行,无需等待任何异步 sftp 函数返回,因为它会立即解析为 undefined。

您没有提及您正在使用的 sftp 库,但假设它返回一个 promise ,您应该能够简单地从 then() 返回这些 promise 。

例如:

.then(() => {
console.log('Finished sftp put for ' + key.replace('.gz', '.dat') + ' file.');
// assumes stfp.put returns a promise, just return it into the chain
return sftp.put(dataStream, key.replace('.gz', '.dat'), true, 'utf-8');
})
<小时/>

根据评论进行编辑:

您应该能够从 then() 调用回调。现在查看编辑中的日志输出,这不是几乎是您在出现错误时所期望的吗 - 它会跳转到捕获点。由于您当时的调用方式,您将获得控制台输出“已连接到 sftp...”。而不是:

.then(console.log('Connected to sftp, starting sftp put for ' + key.replace('.gz', '.dat') + ' file.'))

可能应该是:

.then(() => console.log('Connected to sftp, starting sftp put for ' + key.replace('.gz', '.dat') + ' file.'))

按照您的方式,控制台将在错误从 sftp 返回之前进行记录。

关于node.js - 错误回调不用于node.js lambda中的ssh2-sftp-client的链接then(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47721476/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com