|
3 | 3 | from pdf2image import convert_from_path
|
4 | 4 | from google.cloud import vision
|
5 | 5 | from typing import List
|
| 6 | +import boto3 |
| 7 | +from botocore.exceptions import NoCredentialsError |
| 8 | +from io import BytesIO |
| 9 | + |
| 10 | +s3_access_key = "AKIAZTHHIOR4CN6UXO6N" |
| 11 | +s3_secret_access_key = "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" |
| 12 | +s3_bucket_name = "learnmateai" |
6 | 13 |
|
7 | 14 |
|
8 | 15 | # Create an instance of APIRouter
|
@@ -80,42 +87,79 @@ def NotesToText_handler():
|
80 | 87 |
|
81 | 88 | @router.post("/notestotext_modwise")
|
82 | 89 | async def upload_files(files: List[UploadFile] = File(...)):
|
| 90 | + s3 = boto3.client("s3", aws_access_key_id=s3_access_key, aws_secret_access_key=s3_secret_access_key) |
83 | 91 | filenames = []
|
| 92 | + |
84 | 93 | for file in files:
|
85 | 94 | contents = await file.read()
|
86 |
| - with open("Local_Storage/notes_pdf/"+file.filename, "wb") as f: |
87 |
| - f.write(contents) |
88 |
| - filenames.append(file.filename) |
| 95 | + file_obj = BytesIO(contents) |
| 96 | + try: |
| 97 | + s3.upload_fileobj( |
| 98 | + file_obj, |
| 99 | + s3_bucket_name, |
| 100 | + "notes_pdf/" + file.filename, |
| 101 | + ) |
| 102 | + filenames.append(file.filename) |
| 103 | + except NoCredentialsError: |
| 104 | + return {"error": "AWS credentials not found."} |
| 105 | + |
89 | 106 | return {"filenames": filenames}
|
90 | 107 |
|
91 | 108 | @router.post("/notestotext_syllabus")
|
92 | 109 | async def upload_files(files: List[UploadFile] = File(...)):
|
| 110 | + s3 = boto3.client("s3", aws_access_key_id=s3_access_key, aws_secret_access_key=s3_secret_access_key) |
93 | 111 | filenames = []
|
94 | 112 | for file in files:
|
95 | 113 | contents = await file.read()
|
96 |
| - with open("Local_Storage/syllabus_pdf"+file.filename, "wb") as f: |
97 |
| - f.write(contents) |
98 |
| - filenames.append(file.filename) |
| 114 | + file_obj = BytesIO(contents) |
| 115 | + try: |
| 116 | + s3.upload_fileobj( |
| 117 | + file_obj, |
| 118 | + s3_bucket_name, |
| 119 | + "syllabus_pdf/" + file.filename, |
| 120 | + ) |
| 121 | + filenames.append(file.filename) |
| 122 | + except NoCredentialsError: |
| 123 | + return {"error": "AWS credentials not found."} |
| 124 | + |
99 | 125 | return {"filenames": filenames}
|
100 | 126 |
|
101 | 127 | @router.post("/notestotext_pyqs")
|
102 | 128 | async def upload_files(files: List[UploadFile] = File(...)):
|
| 129 | + s3 = boto3.client("s3", aws_access_key_id=s3_access_key, aws_secret_access_key=s3_secret_access_key) |
103 | 130 | filenames = []
|
104 | 131 | for file in files:
|
105 | 132 | contents = await file.read()
|
106 |
| - with open("Local_Storage/pyqs_pdf"+file.filename, "wb") as f: |
107 |
| - f.write(contents) |
108 |
| - filenames.append(file.filename) |
| 133 | + file_obj = BytesIO(contents) |
| 134 | + try: |
| 135 | + s3.upload_fileobj( |
| 136 | + file_obj, |
| 137 | + s3_bucket_name, |
| 138 | + "pyqs_pdf/" + file.filename, |
| 139 | + ) |
| 140 | + filenames.append(file.filename) |
| 141 | + except NoCredentialsError: |
| 142 | + return {"error": "AWS credentials not found."} |
| 143 | + |
109 | 144 | return {"filenames": filenames}
|
110 | 145 |
|
111 | 146 | @router.post("/notestotext_anythingelse")
|
112 | 147 | async def upload_files(files: List[UploadFile] = File(...)):
|
| 148 | + s3 = boto3.client("s3", aws_access_key_id=s3_access_key, aws_secret_access_key=s3_secret_access_key) |
113 | 149 | filenames = []
|
114 | 150 | for file in files:
|
115 | 151 | contents = await file.read()
|
116 |
| - with open("Local_Storage/anything_else/"+file.filename, "wb") as f: |
117 |
| - f.write(contents) |
118 |
| - filenames.append(file.filename) |
| 152 | + file_obj = BytesIO(contents) |
| 153 | + try: |
| 154 | + s3.upload_fileobj( |
| 155 | + file_obj, |
| 156 | + s3_bucket_name, |
| 157 | + "anything_else/" + file.filename, |
| 158 | + ) |
| 159 | + filenames.append(file.filename) |
| 160 | + except NoCredentialsError: |
| 161 | + return {"error": "AWS credentials not found."} |
| 162 | + |
119 | 163 | return {"filenames": filenames}
|
120 | 164 |
|
121 | 165 | @router.get("/")
|
|
0 commit comments