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