Skip to content

Commit ec8d0e8

Browse files
committed
Create file_sender.py
1 parent e8eab4f commit ec8d0e8

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Backend/file_sender.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import uvicorn
2+
from fastapi import FastAPI
3+
from fastapi import APIRouter
4+
import boto3
5+
6+
7+
aws_access_key_id = 'AKIAZTHHIOR4JJ5HLTUB'
8+
aws_secret_access_key = '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'
9+
bucket_name = 'learnmateai'
10+
11+
# Create an S3 client
12+
s3_client = boto3.client("s3",
13+
aws_access_key_id=aws_access_key_id,
14+
aws_secret_access_key=aws_secret_access_key)
15+
getfiles = APIRouter()
16+
17+
@getfiles.post("/get_notes_txt")
18+
async def retrieve_files(email: str):
19+
# Configure your AWS credentials and region
20+
21+
22+
# Retrieve files from the S3 bucket
23+
24+
prefix = f"{email}/notes_txt"
25+
26+
response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=prefix)
27+
files = []
28+
29+
if "Contents" in response:
30+
for obj in response["Contents"]:
31+
file_key = obj["Key"]
32+
file_obj = s3_client.get_object(Bucket=bucket_name, Key=file_key)
33+
file_content = file_obj["Body"].read().decode("utf-8")
34+
files.append({"name": file_key, "content": file_content})
35+
36+
return {"files": files}
37+
38+
@getfiles.post("/get_notes_pdf")
39+
async def retrieve_files(email: str):
40+
# Configure your AWS credentials and region
41+
42+
43+
# Retrieve files from the S3 bucket
44+
45+
prefix = f"{email}/notes_pdf"
46+
47+
response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=prefix)
48+
files = []
49+
50+
if "Contents" in response:
51+
for obj in response["Contents"]:
52+
file_key = obj["Key"]
53+
file_obj = s3_client.get_object(Bucket=bucket_name, Key=file_key)
54+
file_content = file_obj["Body"].read().decode("utf-8")
55+
files.append({"name": file_key, "content": file_content})
56+
57+
return {"files": files}

0 commit comments

Comments
 (0)