简体   繁体   English

如何从S3存储桶读取大型XML文件,然后使用AWS Lambda将其用作HTTP请求正文

[英]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.

相关问题 使用节点 fs 从 aws s3 存储桶读取文件 - Read file from aws s3 bucket using node fs 如何从 s3 存储桶解析 CSV 以在 javascript AWS Lambda function 中使用 - How to parse CSVs from s3 bucket to use in a javascript AWS Lambda function 使用节点中 aws s3 存储桶的范围读取部分视频文件 - Read partial video file using range from aws s3 bucket in node 如何使用 AWS SDK(在 Node.js 中)中的 putObject() 将文件从 HTTP 响应正文上传到 S3? - How can I upload a file from an HTTP response body to S3 using putObject() from the AWS SDK (in Node.js)? 如何在没有 Aws 密钥和秘密的情况下从 S3 公共存储桶读取 zip 文件 - How to read zip file from S3 public bucket without Aws key and secret 从S3存储桶读取大文件 - Reading a large file from S3 bucket 尝试从 Nodejs 中的 AWS Lambda 函数读取 S3 存储桶的内容时未获得结果 - Not getting results when attempting to read the content of an S3 bucket from AWS Lambda function in Nodejs 使用 lambda 扫描 AWS S3 存储桶中的文件以查找病毒 - Scan files in AWS S3 bucket for virus using lambda 在 AWS Lambda 中使用 Async/Await 写入 S3 存储桶 - Write to S3 bucket using Async/Await in AWS Lambda AWS S3 Lambda事件-从同一存储桶中获取,更新然后放入JSON文件 - AWS S3 Lambda event - get, update, then put JSON file from same bucket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM