我编写了 lambda 函数,每当在 s3 存储桶上创建任何新对象时都会检索 s3 Url。检索 s3Url 后,此 lambda 通过 REST 调用向我的服务器发出请求。
我观察了云观察者。它无法向我的服务器发送请求我不想在我的 lambda 中使用外部包,我想让它轻量级,这就是我使用 nodeJ 的 https 的原因。
这是我的 Lambda 代码
exports.handler = (event,context,callback) => {
// Extract S3 Url and id From S3 object present in event
const https = require('https');
let {s3 , awsRegion} = event["Records"][0];
let {object : {key}, bucket : {name}} = s3;
let s3URL = `https://${name}.s3.${awsRegion}.amazonaws.com/${key}`;
console.log("sURL",s3URL);
let _id = key.split('/')[0];
console.log("id",_id);
//Making http request to my server
let body='';
// the post options
let optionsPost = {
host: 'xyz.abc.com',
path: '/api/v1/apipath',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'XYZ',
'id' : _id
}
};
const keepAliveAgent = new https.Agent({ keepAlive: true });
optionsPost.agent = keepAliveAgent;
let reqPost = https.request(optionsPost, function(res) {
console.log("statusCode: ", res.statusCode);
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
console.log("Result", body.toString());
context.succeed("Sucess")
});
res.on('error', function () {
console.log("Result Error", body.toString());
context.done(null, 'FAILURE');
callback(new Error('failure')) // to return error
});
});
reqPost.write(JSON.stringify({ s3Url: s3URL,userName: 'SYSTEM' }));
reqPost.end();
};
这是我的云观察者错误
{ “errorType”:“错误”,“errorMessage”:“连接 ETIMEDOUT 34.255.225.41:443”,“代码”:“ETIMEDOUT”,“errno”:“ETIMEDOUT”,“系统调用”:“连接”,“地址” :“34.255.225.41”,“端口”:443,“堆栈”:[“错误:连接 ETIMEDOUT 34.255.225.41:443”,“在 TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)”] }