-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmain.py
More file actions
50 lines (41 loc) · 1.77 KB
/
main.py
File metadata and controls
50 lines (41 loc) · 1.77 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
import json
import os
EFS_MOUNT = "/mnt/efs"
def handler(event, context):
"""
Lambda handler for testing EFS filesystem mounting
"""
path = event.get("path", "")
if path == "/read-file":
# Read a file from mounted EFS
filename = event.get("queryStringParameters", {}).get("filename", "")
try:
filepath = os.path.join(EFS_MOUNT, filename)
with open(filepath, "r") as f:
content = f.read()
return {
"statusCode": 200,
"body": json.dumps({"success": True, "content": content, "filename": filename}),
}
except Exception as e:
return {"statusCode": 500, "body": json.dumps({"success": False, "error": str(e)})}
elif path == "/write-file":
# Write a file to mounted EFS
try:
body = json.loads(event.get("body", "{}"))
filename = body.get("filename", "")
content = body.get("content", "")
filepath = os.path.join(EFS_MOUNT, filename)
with open(filepath, "w") as f:
f.write(content)
return {"statusCode": 200, "body": json.dumps({"success": True, "filename": filename})}
except Exception as e:
return {"statusCode": 500, "body": json.dumps({"success": False, "error": str(e)})}
elif path == "/list-files":
# List files in mounted EFS
try:
files = os.listdir(EFS_MOUNT)
return {"statusCode": 200, "body": json.dumps({"success": True, "files": files, "mount_path": EFS_MOUNT})}
except Exception as e:
return {"statusCode": 500, "body": json.dumps({"success": False, "error": str(e)})}
return {"statusCode": 404, "body": json.dumps({"error": "Not found"})}