简体   繁体   中英

Handling S3 Bucket Trigger Event in Lambda Using Python

The AWS Lambda handler has a signature of

def lambda_handler(event, context):

However, I cannot find any documentation as to the event's structure when the trigger is an S3 Bucket receiving a put

I thought that it might be defined in the s3 console, but couldn't find that there.

Anyone have any leads?

The event from S3 to Lambda function will be in json format as shown below,

{  
   "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
      }
   ]
}

here is the link for aws documentation which can guide you. http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html

I think your easiest route is just to experiment quickly:

  1. Create a bucket using the console
  2. Create a lambda that is triggered by puts to the bucket using the console
  3. Ensure you choose the default execution role, so you create cloudwatch logs
  4. The lambda function just needs to "print(event)" when called, which is then logged
  5. Save an object to the bucket

You'll then see the event structure in the log - its pretty self explanatory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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