Created
September 9, 2024 11:30
-
-
Save phainamikaze/53fc893d0a43d78e1afba08fe752828e to your computer and use it in GitHub Desktop.
lambda_challenge_lab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import boto3 | |
def lambda_handler(event, context): | |
# TODO implement | |
for e in event['Records']: | |
bucket_name = e['s3']['bucket']['name'] | |
object_key = e['s3']['object']['key'] | |
print(bucket_name) | |
print(object_key) | |
s3 = boto3.resource('s3') | |
obj = s3.Object(bucket_name,object_key) | |
filecontent = obj.get()['Body'].read().decode('utf-8') | |
word_list = filecontent.split(" ") | |
count = len(word_list) | |
print(count) | |
email_body = "The word count in the {} file is {}. ".format(object_key,count) | |
print(email_body) | |
sns = boto3.client("sns") | |
sns.publish( | |
TopicArn = "arn:aws:sns:us-west-2:620658969040:sendemail", | |
Message = email_body, | |
Subject = "Word Count Result" | |
) | |
return { | |
'statusCode': 200, | |
'body': json.dumps('Hello from Lambda!') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment