S3 Event notifications type Post not send object key to sqs

0

Hi everyone

I setup s3 Event notifications for action upload image to folder "images/" and config sqs queue to lambda. After print event in lambda it's doesn't have object key I upload. And I retry upload another image to s3 it not trigger lambda. But If I Event notifications type "All object create events" it's have object key. How can I config event type Post

Thanks for reading !

1 Answer
0
Accepted Answer

Hi nam nguyen,

Please try this below solution I hope it will help to resolve your issue.

S3 Bucket Event Configuration:

  • Go to the AWS S3 console.
  • Select your bucket and go to the "Properties" tab.
  • Scroll down to the "Event notifications" section.
  • Click on "Create event notification".

Event Configuration:

  • Give your event a name.
  • Select the event types. For your case, you should choose "PUT" to capture object upload events.
  • In the prefix field, enter images/ to filter events only for objects in the images folder.
  • Leave the suffix field empty unless you want to filter for specific file types (e.g., .jpg, .png).
  • Choose the SQS queue as the destination.

SQS Queue:

  • Ensure the SQS queue is properly configured and has the necessary permissions to receive messages from the S3 bucket.
  • The SQS queue should have a policy that allows S3 to send messages to it. The policy should look something like this:

{
    "Version": "2012-10-17",
    "Id": "PolicyID",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "s3.amazonaws.com"
            },
            "Action": "SQS:SendMessage",
            "Resource": "arn:aws:sqs:region:account-id:queue-name",
            "Condition": {
                "ArnLike": {
                    "aws:SourceArn": "arn:aws:s3:::your-bucket-name"
                }
            }
        }
    ]
}

Lambda Function:

  • Ensure your Lambda function is correctly configured to trigger on messages from the SQS queue.
  • The Lambda function's IAM role must have permission to poll the SQS queue and access the S3 bucket.

Test the Setup:

  • Upload an image to the images/ folder in your S3 bucket and check if the Lambda function is triggered.
  • In your Lambda function, log the event to verify if the object key is present.

Here’s an example Lambda function to print the event:


import json

def lambda_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))
    
    # Extract S3 object key
    for record in event['Records']:
        s3_object_key = record['s3']['object']['key']
        print(f"Object key: {s3_object_key}")
    
    return {
        'statusCode': 200,
        'body': json.dumps('Event processed successfully')
    }

Troubleshooting Tips:

  • Verify Permissions: Ensure all necessary permissions are set correctly for the S3 bucket, SQS queue, and Lambda function.

  • Check Logs: Use CloudWatch Logs to check the logs of your Lambda function for any errors or to verify the received event.

  • Test with "All object create events": As you mentioned, the object key is available when using "All object create events". This indicates that your basic setup is correct. The issue might be related to the specific event configuration or filter.

EXPERT
answered 7 days ago
profile picture
EXPERT
A_J
reviewed 7 days ago
  • Thanks. I have change event type to "Put" and now have object key info. But I have process in lambda for this image and overwrite image with same name. If I use Put its will trigger my lambda continuous, anyway solution for "Put" still get object key ? Thanks