使用Python处理Lambda中的S3 Bucket Trigger事件

时间:2017-06-01 21:16:55

标签: python amazon-web-services amazon-s3 aws-lambda

AWS Lambda处理程序具有

的签名
def lambda_handler(event, context):

但是,当触发器是S3 Bucket接收放置时,我找不到关于事件结构的任何文档

我认为它可能在s3控制台中定义,但在那里找不到。

任何人都有任何线索?

3 个答案:

答案 0 :(得分:6)

从S3到Lambda函数的事件将采用json格式,如下所示,

{  
   "Records":[  
      {  
         "eventVersion":"2.0",
         "eventSource":"aws:s3",
         "awsRegion":"us-east-1",
         "eventTime":The time, in ISO-8601 format, for example, 1970-01-01T00:00:00.000Z, when S3 finished processing the request,
         "eventName":"event-type",
         "userIdentity":{  
            "principalId":"Amazon-customer-ID-of-the-user-who-caused-the-event"
         },
         "requestParameters":{  
            "sourceIPAddress":"ip-address-where-request-came-from"
         },
         "responseElements":{  
            "x-amz-request-id":"Amazon S3 generated request ID",
            "x-amz-id-2":"Amazon S3 host that processed the request"
         },
         "s3":{  
            "s3SchemaVersion":"1.0",
            "configurationId":"ID found in the bucket notification configuration",
            "bucket":{  
               "name":"bucket-name",
               "ownerIdentity":{  
                  "principalId":"Amazon-customer-ID-of-the-bucket-owner"
               },
               "arn":"bucket-ARN"
            },
            "object":{  
               "key":"object-key",
               "size":object-size,
               "eTag":"object eTag",
               "versionId":"object version if bucket is versioning-enabled, otherwise null",
               "sequencer": "a string representation of a hexadecimal value used to determine event sequence, 
                   only used with PUTs and DELETEs"            
            }
         }
      },
      {
          // Additional events
      }
   ]
}

这是aws文档的链接,可以指导您。 http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html

答案 1 :(得分:1)

我认为您最简单的方法就是快速试验:

  1. 使用控制台创建存储桶
  2. 使用控制台
  3. 创建一个由存储桶触发的lambda
  4. 确保选择默认执行角色,以便创建cloudwatch日志
  5. lambda函数在调用时只需要“print(event)”,然后记录
  6. 将对象保存到存储桶
  7. 然后,您将在日志中看到事件结构 - 它非常自我解释。

答案 2 :(得分:-1)