0

this code shows all the objects uploaded in the bucket testbucket-101 and no api is being called. Basically the output json of the s3 event should be posted to a different API.

import boto3
import os

def lambda_handler(event, context):
    s3 = boto3.resource('s3')
    bucket=s3.Bucket('testbucket-101')
    for key in bucket.objects.all():
        print(key.key)

1 Answers1

1

You can Configuring Amazon S3 event notifications and register your lambda as the destination.

Even you can parse and extract the object name and bucket. Eventually, download the object and invoke the API

You'll receive an event somewhat like this Tutorial: Using AWS Lambda with Amazon S3

{
  "Records":[
    {
      "eventVersion":"2.0",
      "eventSource":"aws:s3",
      "awsRegion":"us-west-2",
      "eventTime":"1970-01-01T00:00:00.000Z",
      "eventName":"ObjectCreated:Put",
      "userIdentity":{
        "principalId":"AIDAJDPLRKLG7UEXAMPLE"
      },
      "requestParameters":{
        "sourceIPAddress":"127.0.0.1"
      },
      "responseElements":{
        "x-amz-request-id":"C3D13FE58DE4C810",
        "x-amz-id-2":"FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"
      },
      "s3":{
        "s3SchemaVersion":"1.0",
        "configurationId":"testConfigRule",
        "bucket":{
          "name":"sourcebucket",
          "ownerIdentity":{
            "principalId":"A3NL1KOZZKExample"
          },
          "arn":"arn:aws:s3:::sourcebucket"
        },
        "object":{
          "key":"HappyFace.jpg",
          "size":1024,
          "eTag":"d41d8cd98f00b204e9800998ecf8427e",
          "versionId":"096fKKXTRTtl3on89fVO.nfljtsv6qko"
        }
      }
    }
  ]
}

Enabling and configuring event notifications for an S3 Bucket

samtoddler
  • 8,463
  • 2
  • 26
  • 21