-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamo_operations.py
89 lines (83 loc) · 2.53 KB
/
dynamo_operations.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
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
import boto3
import streamlit as st
# Initialize AWS DyanmoDB
dynamodb = boto3.resource('dynamodb', region_name=st.secrets['awsRegion'], aws_access_key_id=st.secrets['accessKeyId'], aws_secret_access_key=st.secrets['awsSecretKey'])
def create_table():
try:
table = dynamodb.create_table(
TableName='DocShelf',
KeySchema=[
{
'AttributeName': 'email',
'KeyType': 'HASH'
},
{
'AttributeName': 'file_name',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'email',
'AttributeType': 'S'
},
{
'AttributeName': 'file_name',
'AttributeType': 'S'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
table.meta.client.get_waiter('table_exists').wait(TableName='DocShelf')
print("Table created successfully")
return table
except Exception as e:
st.error(f"Error creating table: {e}")
print(f"Error creating table: {e}")
return None
def put_item(email, file_name, file_type):
try:
table = dynamodb.Table('DocShelf')
response = table.put_item(
Item={
'email': email,
'file_name': file_name,
'file_type': file_type
}
)
return True
except Exception as e:
st.error(f"Error adding item: {e}")
return False
def get_items(email):
try:
table = dynamodb.Table('DocShelf')
response = table.query(
KeyConditionExpression='email = :email',
ExpressionAttributeValues={
':email': email
}
)
items = response['Items']
item_keys = [item['file_name'] for item in items]
return item_keys
except Exception as e:
st.error(f"Error getting items: {e}")
return []
def delete_item(email, file_name):
try:
table = dynamodb.Table('DocShelf')
response = table.delete_item(
Key={
'email': email,
'file_name': file_name
}
)
return True
except Exception as e:
st.error(f"Error deleting item: {e}")
return False
# Path: dynamo_operations.py