forked from bluesentry/bucket-antivirus-function
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamo_wrapper.py
48 lines (43 loc) · 1.16 KB
/
dynamo_wrapper.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
import boto3
from uuid import uuid4
dynamodb = boto3.resource('dynamodb')
import os
TABLE_NAME = os.getenv("AV_DEFINITION_DYNAMO_TABLE")
table = dynamodb.Table(TABLE_NAME)
def insert_data(data):
"""
this method is used to insert new data in to DynamoDb
"""
doc_id = str(uuid4())
data["id"] = doc_id
table.put_item(Item=data)
return doc_id
def get_data(query):
"""
the method is used to search data
"""
response = table.get_item(Key=query)
item = response['Item']
print(item)
return item
def update_data(query, data):
"""
this method is used to update the exiting table data
"""
update_expression = 'SET'
expression_att = {}
value_key = ":val"
counter = 0
for key,value in data.iteritems():
counter += 1
val_key = value_key + str(counter)
expression_att[val_key] = value
if "=" in update_expression:
update_expression += ", " + key + " = " + val_key
else:
update_expression += " " + key + " = " + val_key
table.update_item(
Key=query,
UpdateExpression=update_expression,
ExpressionAttributeValues=expression_att
)