Spaces:
Build error
Build error
Commit
·
b2f5c00
1
Parent(s):
2c74019
feat: add metadata attribute to json string format into to headers
Browse files- apis/s3.py +3 -4
- app.py +10 -3
apis/s3.py
CHANGED
|
@@ -33,7 +33,7 @@ class S3:
|
|
| 33 |
return [self.client.generate_presigned_url('get_object', Params={'Bucket': bucket_name, 'Key': key}) for key in image_keys]
|
| 34 |
|
| 35 |
# Function to upload file to S3
|
| 36 |
-
def upload_file(self, bucket_name, file, name,
|
| 37 |
res = Response()
|
| 38 |
try:
|
| 39 |
# self.client.upload_fileobj(
|
|
@@ -49,9 +49,8 @@ class S3:
|
|
| 49 |
Body=file,
|
| 50 |
Bucket=bucket_name,
|
| 51 |
Key=name,
|
| 52 |
-
Metadata=
|
| 53 |
-
|
| 54 |
-
})
|
| 55 |
res.status = 200
|
| 56 |
except botocore.exceptions.ClientError as error:
|
| 57 |
res.status = 429
|
|
|
|
| 33 |
return [self.client.generate_presigned_url('get_object', Params={'Bucket': bucket_name, 'Key': key}) for key in image_keys]
|
| 34 |
|
| 35 |
# Function to upload file to S3
|
| 36 |
+
def upload_file(self, bucket_name, file, name, metadata):
|
| 37 |
res = Response()
|
| 38 |
try:
|
| 39 |
# self.client.upload_fileobj(
|
|
|
|
| 49 |
Body=file,
|
| 50 |
Bucket=bucket_name,
|
| 51 |
Key=name,
|
| 52 |
+
Metadata=metadata
|
| 53 |
+
)
|
|
|
|
| 54 |
res.status = 200
|
| 55 |
except botocore.exceptions.ClientError as error:
|
| 56 |
res.status = 429
|
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from http import HTTPStatus
|
| 2 |
-
import os, io, base64
|
| 3 |
from typing import Optional
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
from typing_extensions import Annotated
|
|
@@ -68,7 +68,8 @@ def healthcheck():
|
|
| 68 |
async def upload(
|
| 69 |
o: ImageObject,
|
| 70 |
x_request_user: str = Header(...),
|
| 71 |
-
x_api_key: str = Header(...)
|
|
|
|
| 72 |
):
|
| 73 |
res = Response()
|
| 74 |
logging.info("--------------------------------")
|
|
@@ -78,6 +79,11 @@ async def upload(
|
|
| 78 |
key = f'{o.key}/{o.job_no}/{o.name}'
|
| 79 |
logging.info(f'Key for S3 upload: {key}')
|
| 80 |
if o.content is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
try:
|
| 82 |
# Decode base64 content
|
| 83 |
decoded_content = base64.b64decode(o.content)
|
|
@@ -88,7 +94,8 @@ async def upload(
|
|
| 88 |
|
| 89 |
# Upload file object to S3
|
| 90 |
# logging.info(f"Uploading file to S3 bucket '{AWS_S3_BUCKET_NAME}' with key '{key}'")
|
| 91 |
-
|
|
|
|
| 92 |
logging.info("File uploaded successfully")
|
| 93 |
|
| 94 |
except Exception as e:
|
|
|
|
| 1 |
from http import HTTPStatus
|
| 2 |
+
import os, io, base64, json
|
| 3 |
from typing import Optional
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
from typing_extensions import Annotated
|
|
|
|
| 68 |
async def upload(
|
| 69 |
o: ImageObject,
|
| 70 |
x_request_user: str = Header(...),
|
| 71 |
+
x_api_key: str = Header(...),
|
| 72 |
+
metadata: str = Header(...),
|
| 73 |
):
|
| 74 |
res = Response()
|
| 75 |
logging.info("--------------------------------")
|
|
|
|
| 79 |
key = f'{o.key}/{o.job_no}/{o.name}'
|
| 80 |
logging.info(f'Key for S3 upload: {key}')
|
| 81 |
if o.content is not None:
|
| 82 |
+
try:
|
| 83 |
+
metadata = json.loads(metadata)
|
| 84 |
+
except Exception:
|
| 85 |
+
metadata = {}
|
| 86 |
+
|
| 87 |
try:
|
| 88 |
# Decode base64 content
|
| 89 |
decoded_content = base64.b64decode(o.content)
|
|
|
|
| 94 |
|
| 95 |
# Upload file object to S3
|
| 96 |
# logging.info(f"Uploading file to S3 bucket '{AWS_S3_BUCKET_NAME}' with key '{key}'")
|
| 97 |
+
metadata['x-request-id'] = x_request_user
|
| 98 |
+
res = s3client.upload_file(AWS_S3_BUCKET_NAME, file_obj, key, metadata=metadata)
|
| 99 |
logging.info("File uploaded successfully")
|
| 100 |
|
| 101 |
except Exception as e:
|