0

I am using AWS lambda function and nodejs. I tried to insert multiple images in s3 bucket. find the below for your reference.

var AWS = require('aws-sdk');
var fileType = require('file-type');
var bucket = 'testing';
var s3 = new AWS.S3();


exports.handler = (event, context, callback) => {
    var Userid = event['userid'];
    var media = event['media'];

    media.forEach(function(eachrecord){
        var fileBuffer = Buffer.from(eachrecord, 'base64');
        var fileTypeInfo = fileType(fileBuffer);
        var randomstring = require("randomstring");
        var fileName = Userid+'/'+'media/'+`${randomstring.generate()}.${fileTypeInfo.ext}`;
        var ext = fileTypeInfo.ext;

        var params = {
            Body: fileBuffer,
            Key: fileName,
            Bucket: bucket,
            ContentEncoding: 'base64',
            ContentType: fileTypeInfo.mime
        };
        s3.upload(params, function(err, data){
        if(err)
            {
             callback(err, null);
            }
        else
            {
            let response= {body: JSON.stringify(data.Location)};
            let mediaurl =response.body;
            console.log(mediaurl);
            }
        });
    });
context.succeed('done');    
};    

In media, I have send multiple base64 encode array value in event. but how to implement forloop concept and media insert array value into s3 bucket?

10
  • Did you try this :: stackoverflow.com/questions/43662686/… ?? Commented Dec 22, 2019 at 0:08
  • @srinivasy bro in the above solution, they are not using event handler and i have sent media event in array like media : [ base64encode, base64encode, base64encode]. is there any provision like for loop concept Commented Dec 22, 2019 at 0:23
  • @srinivasy i tried to forEach conditoin after var media = event['media'], but it was not working Commented Dec 22, 2019 at 0:25
  • 1
    So for each event do you get a list of files not the name media, if yes, then what happens if you iterate over media after var media = event['media']; then do same process only change will be var fileBuffer = Buffer.from(eachRecord, 'base64'); Commented Dec 22, 2019 at 0:37
  • @srinivasy bro i have one small query, forEach condition working on aws lambda. becoz var media = event['media']; after i tried using forEach(function(eachreacord){remaining code }) but it was not working. Commented Dec 22, 2019 at 0:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.