简体   繁体   English

使用AWS Lambda上传到AWS S3

[英]Upload to AWS S3 using AWS Lambda

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: 我使用带有mongoDB数据库的node.js中的AWS Lambda在AWS S3中上传图像我使用以下代码使用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 在这是代码我已经在aws s3本地成功上传图像,但使用AWS Lambda这个代码不起作用

You need to set the IAM role for lambda to access AWS 您需要为lambda设置IAM角色才能访问AWS

Also you need to create a policy for your S3 bucket 您还需要为S3存储桶创建策略

Then attach the S3 policy to the Lamba IAM role. 然后将S3策略附加到Lamba IAM角色。

Once you do the above your Lambda will be able to access S3. 完成上述操作后,您的Lambda将能够访问S3。

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. 要使用AWS Lambda First,您需要为该pericular lambda函数提供角色,该角色具有对该存储桶的AWS S3的上载权限。 You won't need access key and secret key using AWS Lambda. 您不需要使用AWS Lambda访问密钥和密钥。

From my understanding you want to upload images to AWS S3 bucket from local directory using AWS Lambda. 根据我的理解,您希望使用AWS Lambda从本地目录将图像上传到AWS S3存储桶。

But if you put the same code in AWS Lambda it won't work. 但是如果你在AWS Lambda中放入相同的代码,它将无法工作。 AWS Lambda is a service from AWS, which is deploy on their cloud. AWS Lambda是AWS提供的服务,可在其云上部署。 So it will work remotely as other services. 所以它将作为其他服务远程工作。

First you need to deploy file server from which images can be accessed over IP. 首先,您需要部署文件服务器,从中可以通过IP访问映像。 It can be on premises server. 它可以是本地服务器。 Then give that IP as the path in the code and implement needful changes in the code 然后将该IP作为代码中的路径,并在代码中实现必要的更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM