如何在AWS Lambda函数的async.series中运行async.each?

时间:2018-07-28 15:03:16

标签: javascript node.js amazon-s3 aws-lambda async.js

我们正在AWS Lambda中编写一个无服务器功能,该功能可以执行以下操作:

  1. 从AWS S3提取JSON文件(多个文件)。
  2. 将JSON文件合并为一个文件。
  3. 将新文件从(2)上载回S3。

为处理此流程,我们尝试在Lambda中使用Nodejs中的异步库。这是到目前为止的代码:

'use-strict';

const AWS = require('aws-sdk');
const async = require('async');

const BUCKET = 'sample-bucket';
const s3path = 'path/to/directory';

exports.handler = (event, context, callback) => {
    let filepaths = []; // Stores filepaths
    let all_data = []; // Array to store all data retrieved 
    let s3Client = new AWS.S3({ params: { Bucket: BUCKET } }); // S3 bucket config

    // Runs functions in the series provided to ensure each function runs one after the other
    async.series([
        // Read file IDs from the event and saves in filepaths array
        function(callbackSeries) {
            console.log('Starting function...');
            event.forEach(id => {
                filepaths.push(`${id}.json`);
            });
            console.log('All filenames pushed.');
            callbackSeries();
        },
        // Fetches each file from S3 using filepaths
        // Then pushes objects from each file to the all_data array
        function(callbackSeries) {
            async.each(filepaths, (file, callback) => {
                let params = {
                    'Bucket': BUCKET,
                    'Key': s3path + file
                }
                s3Client.getObject(params, (err, data) => {
                    if(err) {
                        throw err;
                    } else {
                        if(data.Body.toString()) {
                            console.log('Fetching data...');
                            all_data.push(JSON.parse(data.Body.toString()));
                            callback();
                            console.log('Fetched.');
                        }
                    }
                });
            },
            (err) => {
                if (err) throw err;
                else callbackSeries();
            });
        },
        // NOTHING IS BEING EXECUTED AFTER THIS
        // Puts object back into bucket as one merged file
        function(callbackSeries) {
            console.log('Final function.')

            // Concatenates multiple arrays in all_data into a single array
            all_data = Array.prototype.concat(...all_data);
            let params = {
                'Bucket': BUCKET,
                'Body': JSON.stringify(all_data),
                'Key': 'obj.json'
            };
            s3Client.putObject(params, (err, data) => {
                if(err) throw err;
                else console.log('Success!');
            })
        }
    ], (err) => {
        if(err) throw err;
        else console.log('End.');
    })
}

async.series 中的前两个功能正在正常运行,并且所有console.log语句均已执行。但是我们的第三个功能是:

// Puts object back into bucket as one merged file
function(callbackSeries) {
    console.log('Final function.')

    // Concatenates multiple arrays in all_data into a single array
    all_data = Array.prototype.concat(...all_data);
    let params = {
        'Bucket': BUCKET,
        'Body': JSON.stringify(all_data),
        'Key': 'obj.json'
    };
    s3Client.putObject(params, (err, data) => {
        if(err) throw err;
        else console.log('Success!');
    })
}

根本没有执行。我们添加了console.log语句,但它似乎根本没有执行。

我已经查阅了AWS Support和async的文档,但似乎无法找出问题所在。

任何帮助将不胜感激。

非常感谢。

0 个答案:

没有答案