1

I have image upload in AWS S3 using AWS Lambda in node.js with mongoDB database i have use following code to upload in image using aws s3:

    var fs = require('fs');
    var AWS = require('aws-sdk');
    var config = require('../../server/config');
    var Busboy = require('busboy');
    var busboyBodyParser = require('busboy-body-parser');
    AWS.config.update({ 
      accessKeyId: config.aws.accessKeyId,
      secretAccessKey: config.aws.secretAccessKey,
      region: config.aws.region
    });
    var s3 = new AWS.S3();

module.exports = function(app) {
    app.use(busboyBodyParser());

    app.post('/upload', function(req,res){
        var directory = req.body.directory;  
        var image = req.files.file.name;
        var contenttype = req.files.file.mimetype;
        if(req.body.directory) {
            var file = directory+'/'+image;
        } else {
            var file = image;
        }
        var data = req.files.file.data;
        var keys = {
            Bucket: req.body.bucket,
            Key: file,
            Body: data,
            ACL: 'public-read',
            ContentType: contenttype
        };
        s3.upload(keys, function(err, result) {
            if (err) {
               res.send({
                    isError:true,
                    status:400,
                    message:"File Not Uplaod",
                    data:err
                });
                } else {
                var data = {
                    Location: result.Location,
                    key:result.key,
                    Bucket:result.Bucket
                };
                res.send({
                    isError:false,
                    status:200,
                    message:"File Uplaod",
                    data:data
                });
            }
        });
    });
}

In this is code i have upload image in aws s3 locally success but using AWS Lambda this code not work

1
  • when you test the Lambda function, do you get an error? what is the output? Commented Sep 14, 2018 at 4:08

2 Answers 2

3

You need to set the IAM role for lambda to access AWS

Also you need to create a policy for your S3 bucket

Then attach the S3 policy to the Lamba IAM role.

Once you do the above your Lambda will be able to access S3.

Sign up to request clarification or add additional context in comments.

Comments

1

To use AWS Lambda First you need to provide role to that pericular lambda function, which role has upload permission to AWS S3 of that bucket. You won't need access key and secret key using AWS Lambda.

From my understanding you want to upload images to AWS S3 bucket from local directory using AWS Lambda.

But if you put the same code in AWS Lambda it won't work. AWS Lambda is a service from AWS, which is deploy on their cloud. So it will work remotely as other services.

First you need to deploy file server from which images can be accessed over IP. It can be on premises server. Then give that IP as the path in the code and implement needful changes in the code

Comments

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.