Spaces:
Build error
Build error
Commit
·
0725aa3
1
Parent(s):
5b26209
fix: request schema
Browse files
app.py
CHANGED
|
@@ -105,35 +105,42 @@ async def upload2(
|
|
| 105 |
job_no: Annotated[str, Form()] = '',
|
| 106 |
s3key: Annotated[str, Form()] = '',
|
| 107 |
ref: Annotated[str, Form()] = '',
|
| 108 |
-
file:
|
| 109 |
):
|
| 110 |
res = Response()
|
|
|
|
|
|
|
| 111 |
logging.info("--------------------------------")
|
| 112 |
logging.info("Received request to upload image")
|
| 113 |
-
|
| 114 |
# Validate headers
|
| 115 |
if not is_valid(x_request_user, x_api_key):
|
| 116 |
res.status = HTTPStatus.FORBIDDEN
|
| 117 |
res.error = "Invalid credentials"
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
file_content = await file.read()
|
| 125 |
-
logging.info(f'File content length: {len(file_content)} bytes')
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
res.data = ref
|
| 137 |
return res
|
| 138 |
|
| 139 |
def is_valid(u, p):
|
|
|
|
| 105 |
job_no: Annotated[str, Form()] = '',
|
| 106 |
s3key: Annotated[str, Form()] = '',
|
| 107 |
ref: Annotated[str, Form()] = '',
|
| 108 |
+
file: UploadFile = File(...)
|
| 109 |
):
|
| 110 |
res = Response()
|
| 111 |
+
res.data = ref
|
| 112 |
+
|
| 113 |
logging.info("--------------------------------")
|
| 114 |
logging.info("Received request to upload image")
|
| 115 |
+
|
| 116 |
# Validate headers
|
| 117 |
if not is_valid(x_request_user, x_api_key):
|
| 118 |
res.status = HTTPStatus.FORBIDDEN
|
| 119 |
res.error = "Invalid credentials"
|
| 120 |
+
return res
|
| 121 |
+
|
| 122 |
+
if file is None:
|
| 123 |
+
res.status = HTTPStatus.BAD_REQUEST
|
| 124 |
+
res.error = "File not found"
|
| 125 |
+
return res
|
| 126 |
|
| 127 |
+
key = f'{s3key}/{job_no}/{file.filename}'
|
| 128 |
+
logging.info(f'Key for S3 upload: {key}')
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
try:
|
| 131 |
+
# Read the file content directly from UploadFile
|
| 132 |
+
file_content = await file.read()
|
| 133 |
+
logging.info(f'File content length: {len(file_content)} bytes')
|
| 134 |
|
| 135 |
+
# Upload file object to S3
|
| 136 |
+
# logging.info(f"Uploading file to S3 bucket '{AWS_S3_BUCKET_NAME}' with key '{key}'")
|
| 137 |
+
res = s3client.upload_file(AWS_S3_BUCKET_NAME, file, key)
|
| 138 |
+
logging.info("File uploaded successfully")
|
| 139 |
+
|
| 140 |
+
except Exception as e:
|
| 141 |
+
res.error = str(e)
|
| 142 |
+
logging.warning(f"Error during upload: {res.error}")
|
| 143 |
|
|
|
|
| 144 |
return res
|
| 145 |
|
| 146 |
def is_valid(u, p):
|