-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathefsReadS3ToSQS.py
More file actions
92 lines (63 loc) · 2.07 KB
/
efsReadS3ToSQS.py
File metadata and controls
92 lines (63 loc) · 2.07 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import json
import boto3
def lambda_handler(event, context):
bucket = "wishyoo-sih-source"
prefix = "dev"
sqsURL = "https://sqs.us-west-2.amazonaws.com/727869158831/process_image_file_to_webp"
startPage = 0
endPage = 2
keysToReturnPerPage = 4
keyNameList = getPaginatorList(bucket, prefix, startPage, endPage, keysToReturnPerPage)
# showKeyNameList(keyNameList)
sendMessageSQS(bucket, sqsURL, keyNameList)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
def showKeyNameList(keyNameList):
print(f"keyCount = {len(keyNameList)}")
if len(keyNameList) <= 10:
for i in range(len(keyNameList)):
print(f"{keyNameList[i]}")
def getPaginatorList(bucket, prefix, startPage, endPage, keysToReturnPerPage):
client = boto3.client('s3')
paginator = client.get_paginator('list_objects')
operation_parameters = {'Bucket': bucket,
'Prefix': prefix}
page_iterator = paginator.paginate(**operation_parameters)
pageCount = 0
keyList = []
for page in page_iterator:
pageCount += 1
lines = page['Contents']
if pageCount >= startPage:
lineCount = 0
for line in lines:
lineCount += 1
key = line['Key']
keyList.append(key)
if lineCount >= keysToReturnPerPage:
break
if pageCount >= endPage:
break
return keyList
def sendMessageSQS(bucket, sqsURL, listNames):
sqs = boto3.client('sqs')
for name in listNames:
response = sqs.send_message(
QueueUrl=sqsURL,
DelaySeconds=10,
MessageAttributes={
'Bucket': {
'DataType': 'String',
'StringValue': bucket
},
'Key': {
'DataType': 'String',
'StringValue': str(name)
}
},
MessageBody=(
str(name)
)
)