S3 trigger with prefix in lambda is not working

0

I have a bucket say s3://example and I have set a trigger for this bucket on every object creation with prefix presets/ and suffix .json. However when I upload files at s3://example/presets/xyz/abc.json. This does not invoke the lambda function.

gefragt vor 8 Monaten644 Aufrufe
1 Antwort
0

According to https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-how-to-filtering.html, the suffix must NOT include the leading "." (dot). So suffix should be json

However, I tested with both (including the leading . and without), and it both worked.

I used the following AWS SAM template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-app

Globals:
  Function:
    Timeout: 3
    MemorySize: 128

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Events:
        BackupEvent:
          Type: S3
          Properties:
            Bucket:
              Ref: MyBucket
            Events: s3:ObjectCreated:*
            Filter:
              S3Key:
                Rules:
                - Name: prefix   
                  Value: presets/
                - Name: suffix   
                  Value: .json  
      Policies:
        - S3ReadPolicy:
            BucketName: MyBucket

  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: examples3eventbucket
    

with the following Lambda function in python:

import json
import urllib.parse


def lambda_handler(event, context):
   print("Start processing S3 Event")
    
   file_name = urllib.parse.unquote_plus(urllib.parse.unquote(event['Records'][0]['s3']['object']['key']))  #S3 event contains file name in URL encoding, needs to be decoded - https://github.com/aws-samples/amazon-textract-enhancer/issues/2
   print("Backup file: " + file_name)

and I uploaded a file using aws s3 cp abc.json s3://examples3eventbucket/presets/xyz/abc.json

and check the Lambda logs using sam logs --stack-name sam-app , you can see the Lambda function was invoked:

2023/10/12/[$LATEST]66b884464faf4c3e98af033f4e8b6272 2023-10-12T08:46:26.569000 INIT_START Runtime Version: python:3.9.v32      Runtime Version ARN: arn:aws:lambda:af-south-1::runtime:65d8a55e7d094cdfca79c506263a99c166ff4afba537e80b4eec2eb9568f7ba6
2023/10/12/[$LATEST]66b884464faf4c3e98af033f4e8b6272 2023-10-12T08:46:26.674000 START RequestId: fff97e6f-d7e9-46dd-93e7-3b07635f6dca Version: $LATEST
2023/10/12/[$LATEST]66b884464faf4c3e98af033f4e8b6272 2023-10-12T08:46:26.674000 Start processing S3 Event
2023/10/12/[$LATEST]66b884464faf4c3e98af033f4e8b6272 2023-10-12T08:46:26.674000 Backup file: presets/xyz/abc.json
2023/10/12/[$LATEST]66b884464faf4c3e98af033f4e8b6272 2023-10-12T08:46:26.675000 END RequestId: fff97e6f-d7e9-46dd-93e7-3b07635f6dca
2023/10/12/[$LATEST]66b884464faf4c3e98af033f4e8b6272 2023-10-12T08:46:26.675000 REPORT RequestId: fff97e6f-d7e9-46dd-93e7-3b07635f6dca  Duration: 1.55 ms       Billed Duration: 2 ms   Memory Size: 128 MB     Max Memory Used: 36 MB  Init Duration: 103.19 ms
profile picture
beantwortet vor 8 Monaten
profile pictureAWS
EXPERTE
überprüft vor 8 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen