Home > Software engineering >  S3.putObject is not able to execute
S3.putObject is not able to execute

Time:11-22

var aws = require('aws-sdk');
var s3 = new aws.S3({ apiVersion: '2006-03-01' });
var bucket = 'ashish-3245';
var acl = 'public-read';
var s3prefix = 'DeepSecurityEvents/';
var ext = '.json';

exports.handler = async(event, context) => {
    //console.log('Received event:', JSON.stringify(event, null, 2));
    const message = event.Records[0].Sns.Message;
    console.log("records", message);

    var timestamp = event.Records[0].Sns.Timestamp;
    var date = getFormattedDate(new Date(timestamp));
    console.log('from date', date);

    //add 5 random digits to prevent overwriting an existing file if two messages are received at the same ms.
    var random5digits = Math.floor(Math.random() * 90000)   10000;
    //ie. DeepSecurityEvents/2016/07/20/2016-07-20T15:30:00.000Z12345.json
    var filename = s3prefix   date   '/'   timestamp   random5digits   ext;


    putObjectToS3(message, filename);
    console.log('From S3:', message);
    console.log(filename);

};

function getFormattedDate(d) {
    //returns yyyy/MM/dd
    return d.getFullYear()   '/'   twoDigits(d.getMonth()   1)   '/'   twoDigits(d.getDate());
}

function twoDigits(n) {
    return n < 10 ? '0'   n : n;
}

function putObjectToS3(message, filename) {
    console.log('From async:', message);
    var params = { Bucket: bucket, Key: filename, ACL: acl, Body: message };
    s3.putObject(params, function(err, message) {
        console.log('p SNS:', message);
        if (err) console.log(err, err.stack);
        else console.log(message);
        context.done();
    });
}

Output

kkk

Response
null

Function Logs
START RequestId: 8d6f4394-9b87-49e5-bbec-ad88cf16813e Version: $LATEST
2021-11-21T11:22:16.379Z    8d6f4394-9b87-49e5-bbec-ad88cf16813e    INFO    records omly message
2021-11-21T11:22:16.380Z    8d6f4394-9b87-49e5-bbec-ad88cf16813e    INFO    from date 1970/01/01
2021-11-21T11:22:16.380Z    8d6f4394-9b87-49e5-bbec-ad88cf16813e    INFO    From async: omly message
2021-11-21T11:22:16.492Z    8d6f4394-9b87-49e5-bbec-ad88cf16813e    INFO    From S3: omly message
2021-11-21T11:22:16.505Z    8d6f4394-9b87-49e5-bbec-ad88cf16813e    INFO    DeepSecurityEvents/1970/01/01/1970-01-01T00:00:00.000Z99418.json
END RequestId: 8d6f4394-9b87-49e5-bbec-ad88cf16813e
REPORT RequestId: 8d6f4394-9b87-49e5-bbec-ad88cf16813e  Duration: 148.84 ms Billed Duration: 149 ms Memory Size: 400 MB Max Memory Used: 78 MB  Init Duration: 415.78 ms

Request ID
8d6f4394-9b87-49e5-bbec-ad88cf16813e

CodePudding user response:

You are using async handler, so your function finishes before your code actually runs. You can wrap your code in a Promise as shown in the docs to overcome the issue.

CodePudding user response:

Your handler is asynchronous, so in order for other asynchronous calls (example: putObject) to being able to finish you either:

  1. Return a promise which wraps your code (see answer from @Marcin)
  2. Use async-await pattern. For this what you have to do is the following:

Refactor putObjectToS3 to return a promise:

function putObjectToS3(message, filename) {
    console.log('From async:', message);
    const params = { Bucket: bucket, Key: filename, ACL: acl, Body: message };
    try {
        return s3.putObject(params).promise();
    } catch(e) {
        console.err(e);
    }
}

In the Lambda handler we must call putObjectToS3 with await as well:

const result = await putObjectToS3(message, filename);
console.log('p SNS:', result);
  • Related