-
Notifications
You must be signed in to change notification settings - Fork 7
/
wordcount.py
55 lines (47 loc) · 1.56 KB
/
wordcount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import boto3
s3_client = boto3.client('s3')
mock_event = {
"Records": [{
"s3": {
"bucket": {
"name": "example-bucket",
},
"object": {
"key": "test%2Fkey",
"size": 1024,
}
}
}]
}
def get_bucket_name(event):
return event["Records"][0]['s3']['bucket']['name']
def get_object_name(event):
return event["Records"][0]['s3']['object']['key']
def get_object_size(event):
return event["Records"][0]['s3']['object']['size']
def wordcount(filedata):
words = filedata.split()
num_words = len(words)
return num_words
def lambda_handler(event, context):
print(f"Bucket: {get_bucket_name(event)}")
print(f"Filename: {get_object_name(event)}")
print(f"File Size: {get_object_size(event)}")
file_name = get_object_name(event)
fixed_file_name = file_name.replace('/', '-')
bucket_name = get_bucket_name(event)
download_path = f"/tmp/{fixed_file_name}"
s3_client.download_file(bucket_name, file_name, download_path)
with open(download_path, encoding="utf8") as f:
filedata = f.read()
num_words = wordcount(filedata)
print(num_words)
return f"The word count in the file {download_path} is {num_words}."
"""if __name__ == "__main__":
test = wordcount("HelloWorld Accurate COunt")
print(test)
with open("book.txt", encoding="utf8") as f:
filedata = f.read()
num_words = wordcount(filedata)
print(num_words)
"""