我正在尝试在S3中处理上传的文件.由于getObject是异步主函数在处理完成之前结束,因此AWS在3-4秒内杀死lambda.
更糟糕的是,处理方法中还包含异步操作 - 它会进行http调用.
在高级别,我的代码看起来像:
exports.handler = function(event, context) {
// Get the object from the event and show its content type
var bucket = event.Records[0].s3.bucket.name;
var key = event.Records[0].s3.object.key;
var params = {
Bucket: bucket,
Key: key
};
s3.getObject(params, function(err, data) {
if (err) {
...
} else {
processFile(data.Body.toString(), 0);
console.log("ok");
}
});
//need to wait here till processFile is done
};
processFile = function(content, start) {
... build url to call
http.get(url, function(res) {
console.log("Got …Run Code Online (Sandbox Code Playgroud) 我在使用一些node.js代码时遇到了一些问题,我将其放入AWS Lambda.我需要做几个异步调用,虽然第一个是我期望的行为,但lambda函数在第二个调用完成之前终止.
返回值为null,这让我认为lambda正在触及它的隐式回调,但是我认为它不应该这样做,而有一个尚未解决的承诺.
码:
exports.handle = async function(event, context) {
var AWS = require("aws-sdk");
AWS.config.update({
region: "eu-west-1",
endpoint: "dynamodb.eu-west-1.amazonaws.com"
});
var docClient = new AWS.DynamoDB.DocumentClient();
console.log("Scanning Catalogue...");
var params = {
TableName : "Catalogue"
};
await docClient.scan(params).promise().then(function (data) {
console.log("Scan succeeded.");
data.Items.forEach(function (item) {
//console.log(item.url);
checkIfUpdateRequired(item);
})
})
}
async function checkIfUpdateRequired (catalogueItem) {
var request = require("request-promise");
console.log("Getting " + catalogueItem.url);
await request(catalogueItem.url).then(function(response) {
console.log(response.statusCode);
console.log(response.headers['content-type']);
});
}
Run Code Online (Sandbox Code Playgroud)
日志输出:
START RequestId: 634a55b7-6258-11e8-9f18-6b300c3b5de1 Version: $LATEST
2018-05-28T09:20:44.425Z 634a55b7-6258-11e8-9f18-6b300c3b5de1 Scanning Catalogue...
2018-05-28T09:20:45.446Z …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的 lambda 函数(nodeJS),它将接收到的事件放入 kinesis 流中。这是源代码:
'use strict';
const AWS = require('aws-sdk');
const kinesis = new AWS.Kinesis({apiVersion: '2013-12-02'});
exports.handler = async (event, context, callback) => {
let body = JSON.parse(event.body);
let receptionDate = new Date().toISOString();
let partitionKey = "pKey-" + Math.floor(Math.random() * 10);
// Response format needed for API Gateway
const formatResponse = (status, responseBody) => {
return {
statusCode: status,
headers: { "Content-Type": "application/json" },
body: JSON.stringify(responseBody)
}
}
// body.events is an array of events. Just add the reception …Run Code Online (Sandbox Code Playgroud)