2024-06-12 14:47:49 +05:30
|
|
|
import boto3
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
2025-03-26 21:19:33 -01:00
|
|
|
def lambda_handler(event, context):
|
|
|
|
|
# i want to know that event thing
|
|
|
|
|
print(event)
|
2024-06-12 14:47:49 +05:30
|
|
|
|
2025-03-26 21:19:33 -01:00
|
|
|
# extract relevant information from the s3 event trigger
|
|
|
|
|
bucket_name = event['Records'][0]['s3']['bucket']['name']
|
|
|
|
|
object_key = event['Records'][0]['s3']['object']['key']
|
2024-06-12 14:47:49 +05:30
|
|
|
|
2025-03-26 21:19:33 -01:00
|
|
|
# perform desired operations with the uploaded file
|
|
|
|
|
print(f"File '{object_key}' was uploaded to bucket '{bucket_name}'")
|
2024-06-12 14:47:49 +05:30
|
|
|
|
2025-03-26 21:19:33 -01:00
|
|
|
# example: send a notification via SNS
|
|
|
|
|
sns_client = boto3.client('sns')
|
|
|
|
|
topic_arn = 'arn:aws:sns:us-east-1:<account-id>:s3-lambda-sns'
|
|
|
|
|
sns_client.publish(
|
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
|
Subject='s3 object created !!',
|
|
|
|
|
Message=f"File '{object_key}' was uploaded to bucket '{bucket_name}'"
|
|
|
|
|
)
|
2024-06-12 14:47:49 +05:30
|
|
|
|
2025-03-26 21:19:33 -01:00
|
|
|
# Example: Trigger another Lambda function
|
|
|
|
|
# lambda_client = boto3.client('lambda')
|
|
|
|
|
# target_function_name = 'my-another-lambda-function'
|
|
|
|
|
# lambda_client.invoke(
|
|
|
|
|
# FunctionName=target_function_name,
|
|
|
|
|
# InvocationType='Event',
|
|
|
|
|
# Payload=json.dumps({'bucket_name': bucket_name, 'object_key': object_key})
|
|
|
|
|
# )
|
|
|
|
|
# in case of queuing and other objectives similar to the Netflix flow of triggering
|
2024-06-12 14:47:49 +05:30
|
|
|
|
2025-03-26 21:19:33 -01:00
|
|
|
return {
|
|
|
|
|
'statusCode': 200,
|
|
|
|
|
'body': json.dumps("Lambda function executed successfully !!")
|
|
|
|
|
}
|