[英]How to read a large XML file from S3 bucket and then use it as an HTTP request body using AWS Lambda
I'm writing an AWS Lambda, where I need to use the content of an XML file from S3 bucket to make an HTTP PUT request. 我正在编写一个AWS Lambda,在这里我需要使用S3存储桶中XML文件的内容来发出HTTP PUT请求。 This lambda should be triggered whenever there's a file upload in the given S3 bucket. 只要给定的S3存储桶中有文件上传,就应触发该lambda。 Following is my code in Node.js: 以下是我在Node.js中的代码:
exports.handler = (event, context, callback) => {
var s3 = new AWS.S3();
var sourceBucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
const params = {
Bucket: sourceBucket,
Key: key
};
s3.getObject(params, function(err, data) {
if (err)
console.log(err, err.stack); // an error occurred
else {
var body = data.Body.toString();
const params2 = {
url: 'http://url:port/PutFile',
body: body
};
req.put(params2, function(err, res, body) {
if(err){
console.log('------error------', err);
} else{
console.log('------success--------', body);
}
});
req('http://url:port/ResetData', function (error, response, body) {
//Check for error
if(error){
return console.log('Error:', error);
}
//Check for right status code
if(response.statusCode !== 200){
return console.log('Invalid Status Code Returned:', response.statusCode);
}
console.log(body);
});
});
};
My code is working fine and making required calls for a dummy XML file (~3KB). 我的代码运行正常,并且对虚拟XML文件(〜3KB)进行了必要的调用。 But for original requirement, with file size being more than 10MB, its returning following exception: 但是对于原始要求,文件大小超过10MB,则返回以下异常:
RequestId: <someId> Error: Runtime exited with error: signal: killed
I've tried increasing the lambda timeout to 10 minutes and I'm still getting the same error. 我尝试将Lambda超时增加到10分钟,但仍然遇到相同的错误。 How should I resolve this? 我该如何解决?
The issue is likely your lambda is running out of memory and aws killed execution. 问题可能是您的lambda内存不足,aws杀死了执行程序。 If you look at the line above this line: 如果您查看此行上方的行:
...Error: Runtime exited with error: signal: killed ...错误:运行时退出,错误:信号:已终止
you will see the line below (boldface is mine): 您将看到下面的行(黑体字是我的):
REPORT RequestId: XXXX-XXXXX-XXXXX Duration: XXXX.XX ms Billed Duration: XXXXX ms Memory Size: 128 MB Max Memory Used: 129 MB REPORT RequestId:XXXX-XXXXX-XXXXX持续时间:XXXX.XX ms计费持续时间:XXXXX ms内存大小:128 MB使用的最大内存:129 MB
Notice you've used more memory than the size you allocated to the lambda. 请注意,您使用的内存超过了分配给lambda的大小。 Increase the limit & try again. 增加限制并重试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.