文件上传到s3存储桶后如何触发我的Lambda函数 [英] How to trigger my Lambda Function once the file is uploaded to s3 bucket

查看:163
本文介绍了文件上传到s3存储桶后如何触发我的Lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将一个zip文件上传到S3存储桶.我需要触发以下内容 lambda
zip文件上传后即可立即使用.请帮助我 继续

I have upload a zip file to S3 bucket.I need to trigger my below lambda
function immediately once the zip file has been uploaded.Kindly help me how to proceed

exports.handler = function (event, context) { 

MyLambdaFuntion();
}

MyLambdaFuntion()
{
var bucketName = "TestBucket1";
var fileKey = "test.js";
s3.getObject(params, function (err, data) {
if (err)
    console.log(err, err.stack);
   else {
          console.log(data);
       }
 });
}

推荐答案

您需要正确执行一些步骤.

There are some steps you need to follow correctly to do so.

步骤1:首先创建lambda函数,选择runtime,然后从列表中选择blank function或任何蓝图.

step 1: First create your lambda function, select the runtimeand select blank function or any blue print from the list.

步骤2:选择空白方块,然后从服务列表中选择S3.

step 2: Select the blank square and choose S3 from the list of services.

第3步:选择要从中触发的存储桶,然后选择事件类型.您的情况应该是Object Created (All)

step 3: Select the bucket you want to trigger from and choose the event type. In your case it should be Object Created (All)

第4步:输入前缀,以防S3内有任何文件夹,并且只想触发上传到该文件夹​​.

step 4: Enter prefix, incase if you have any folders inside the S3 and want to triggered only uploading to that folder.

第5步:输入后缀,仅针对特定后缀'.jpg'触发

step 5: Enter suffix, to triggered only for the specific suffix '.jpg'

步骤6:勾选启用触发器复选框,然后选择下一步.

step 6: Tick the enable trigger checkbox and choose Next.

步骤7:现在给功能命名和描述.如果要上传代码或在其中自己输入编辑器,请更改代码输入类型.

step 7: Now give the fucntion a Name and description. If you want to upload the code or type in the editor there itself, change code entry type.

步骤8:在Handler函数中,选择index.handler,这是文件上传后将调用的函数名称.索引是文件名,处理程序是函数名.

step 8: In Handler function choose index.handler this is the function name it will call once the file is uploaded. Index is file name and handler is function name.

第9步:选择create a custom role,然后将其定向到一个新页面,保留所有字段不变,不做任何更改,然后选择Allow.

step 9: Choose create a custom role and it directs to a new page there leave all the fields as it is, don't change anything and choose Allow.

步骤10:现在回到旧选项卡,选择角色-> choose from existing role并选择新创建的role name

step 10: Now come back to old tab, Select the role --> choose from existing role and select the newly created role name

步骤11:选择下一步",复查所有选定的选项,然后单击Create Function.

step 11: Select Next, review all the selected options and click Create Function.

成功创建函数后,转到触发"选项卡,您可以看到已配置用于触发的S3存储桶.

Once created the function successfully, then go to trigger tab and you can see the S3 bucket configured for triggering.

现在开始在代码编辑器中编写代码,或将其从本地上传到代码选项卡中的lambda函数.

Now start writing the code in the code editor or upload it from local to the lambda function in code tab.

下面是读取文件的简单S3代码.

Simple S3 code to read a file is below.

var aws = require('aws-sdk'),;

var s3 = new aws.S3({ apiVersion: '2006-03-01', accessKeyId: process.env.ACCESS_KEY, secretAccessKey: process.env.SECRET_KEY, region: process.env.LAMBDA_REGION });


exports.handler = function(event, context, exit){
    //console.log('Received event:', JSON.stringify(event, null, 2));

    // Get the object from the event and show its content type
    const bucket = event.Records[0].s3.bucket.name;
    const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
    const params = {
       Bucket: bucket,
       Key: key,
    };

     s3.getObject(params, function(err, data){
         if (err) {
           console.log('ERROR ' + err);
           exit(err);
         } else {
           // the data has the content of the uploaded file
         }
     });       
};

希望这会有所帮助!

这篇关于文件上传到s3存储桶后如何触发我的Lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆