s3PutObject从Lambda突然停止工作

时间:2018-12-14 04:38:38

标签: node.js amazon-web-services aws-lambda

我的Lambda函数中有此代码

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

const s3 = new AWS.S3();

exports.handler = (event, context, callback) => {

    const bucketName = "dhaval-upload";

    let data = {
        firstname: event.firstname,
        lastname: event.lastname,
        email: event.email,
        userAgent: event.userBrowser,
        userIP: event.userIP
    };

    let params = {
      Body: JSON.stringify(data),
      Bucket: bucketName, 
      Key: event.email
    };

    s3.putObject(params);

    callback(null, { message: event.email});
};

这将返回成功回调,但不会在s3中写入对象。

这是可行的,但由于某种原因现在却没有。

请注意:-我检查了cloudwatch日志,但从中找不到问题的原因。

1 个答案:

答案 0 :(得分:0)

也许您可以这样尝试,应该对代码使用回调/异步

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

const s3 = new AWS.S3();

exports.handler = (event, context, callback) => {

    const bucketName = "dhaval-upload";

    let data = {
        firstname: event.firstname,
        lastname: event.lastname,
        email: event.email,
        userAgent: event.userBrowser,
        userIP: event.userIP
    };

    let params = {
      Body: JSON.stringify(data),
      Bucket: bucketName, 
      Key: event.email
    };

    // s3.putObject(params);

    // callback(null, { message: event.email});
    // === it works like this === 
    s3.putObject(params, (err, data) => {
        if (err) callback(null, err);
        else callback(null, { message: event.email });
    });
};