Spaces:
Build error
Build error
Commit
·
fb20d97
1
Parent(s):
b935292
feat: use client id instead of x-request-user, for x-request-user attacted to s3 metadata
Browse files- apis/s3.py +10 -2
- app.py +7 -5
apis/s3.py
CHANGED
|
@@ -33,10 +33,18 @@ 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
res.status = 200
|
| 41 |
except botocore.exceptions.ClientError as error:
|
| 42 |
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, req_id):
|
| 37 |
res = Response()
|
| 38 |
try:
|
| 39 |
+
self.client.upload_fileobj(
|
| 40 |
+
Fileobj=file,
|
| 41 |
+
Bucket=bucket_name,
|
| 42 |
+
Key=name,
|
| 43 |
+
ExtraArgs={
|
| 44 |
+
'Metadata': {
|
| 45 |
+
'x-request-id': req_id,
|
| 46 |
+
}
|
| 47 |
+
})
|
| 48 |
res.status = 200
|
| 49 |
except botocore.exceptions.ClientError as error:
|
| 50 |
res.status = 429
|
app.py
CHANGED
|
@@ -20,7 +20,7 @@ from fastapi.openapi.docs import (
|
|
| 20 |
load_dotenv()
|
| 21 |
IS_DEV = os.environ.get('ENV', 'DEV') != 'PROD'
|
| 22 |
AWS_S3_BUCKET_NAME = os.getenv('AWS_S3_BUCKET_NAME', '')
|
| 23 |
-
|
| 24 |
X_API_KEY = os.environ.get('X_API_KEY')
|
| 25 |
|
| 26 |
logging.basicConfig(
|
|
@@ -68,6 +68,7 @@ def healthcheck():
|
|
| 68 |
@app.post("/image")
|
| 69 |
async def upload(
|
| 70 |
o: ImageObject,
|
|
|
|
| 71 |
x_request_user: str = Header(...),
|
| 72 |
x_api_key: str = Header(...)
|
| 73 |
):
|
|
@@ -75,7 +76,7 @@ async def upload(
|
|
| 75 |
logging.info("--------------------------------")
|
| 76 |
logging.info("Received request to upload image")
|
| 77 |
|
| 78 |
-
if is_valid(
|
| 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:
|
|
@@ -89,7 +90,7 @@ async def upload(
|
|
| 89 |
|
| 90 |
# Upload file object to S3
|
| 91 |
# logging.info(f"Uploading file to S3 bucket '{AWS_S3_BUCKET_NAME}' with key '{key}'")
|
| 92 |
-
res = s3client.upload_file(AWS_S3_BUCKET_NAME, file_obj, key)
|
| 93 |
logging.info("File uploaded successfully")
|
| 94 |
|
| 95 |
except Exception as e:
|
|
@@ -102,6 +103,7 @@ async def upload(
|
|
| 102 |
|
| 103 |
@app.post("/image-multiparts")
|
| 104 |
async def upload2(
|
|
|
|
| 105 |
x_request_user: str = Header(...),
|
| 106 |
x_api_key: str = Header(...),
|
| 107 |
job_no: Annotated[str, Form()] = '',
|
|
@@ -116,7 +118,7 @@ async def upload2(
|
|
| 116 |
logging.info("Received request to upload image")
|
| 117 |
|
| 118 |
# Validate headers
|
| 119 |
-
if not is_valid(
|
| 120 |
res.status = HTTPStatus.FORBIDDEN
|
| 121 |
res.error = "Invalid credentials"
|
| 122 |
return res.json()
|
|
@@ -146,7 +148,7 @@ async def upload2(
|
|
| 146 |
return res.json()
|
| 147 |
|
| 148 |
def is_valid(u, p):
|
| 149 |
-
return u ==
|
| 150 |
|
| 151 |
if __name__=='__main__':
|
| 152 |
uvicorn.run('app:app', host='0.0.0.0', port=7860, reload=True)
|
|
|
|
| 20 |
load_dotenv()
|
| 21 |
IS_DEV = os.environ.get('ENV', 'DEV') != 'PROD'
|
| 22 |
AWS_S3_BUCKET_NAME = os.getenv('AWS_S3_BUCKET_NAME', '')
|
| 23 |
+
CLIENT_ID = os.environ.get('CLIENT_ID')
|
| 24 |
X_API_KEY = os.environ.get('X_API_KEY')
|
| 25 |
|
| 26 |
logging.basicConfig(
|
|
|
|
| 68 |
@app.post("/image")
|
| 69 |
async def upload(
|
| 70 |
o: ImageObject,
|
| 71 |
+
client_id: str = Header(...),
|
| 72 |
x_request_user: str = Header(...),
|
| 73 |
x_api_key: str = Header(...)
|
| 74 |
):
|
|
|
|
| 76 |
logging.info("--------------------------------")
|
| 77 |
logging.info("Received request to upload image")
|
| 78 |
|
| 79 |
+
if is_valid(client_id, x_api_key):
|
| 80 |
key = f'{o.key}/{o.job_no}/{o.name}'
|
| 81 |
logging.info(f'Key for S3 upload: {key}')
|
| 82 |
if o.content is not None:
|
|
|
|
| 90 |
|
| 91 |
# Upload file object to S3
|
| 92 |
# logging.info(f"Uploading file to S3 bucket '{AWS_S3_BUCKET_NAME}' with key '{key}'")
|
| 93 |
+
res = s3client.upload_file(AWS_S3_BUCKET_NAME, file_obj, key, req_id=x_request_user)
|
| 94 |
logging.info("File uploaded successfully")
|
| 95 |
|
| 96 |
except Exception as e:
|
|
|
|
| 103 |
|
| 104 |
@app.post("/image-multiparts")
|
| 105 |
async def upload2(
|
| 106 |
+
client_id: str = Header(...),
|
| 107 |
x_request_user: str = Header(...),
|
| 108 |
x_api_key: str = Header(...),
|
| 109 |
job_no: Annotated[str, Form()] = '',
|
|
|
|
| 118 |
logging.info("Received request to upload image")
|
| 119 |
|
| 120 |
# Validate headers
|
| 121 |
+
if not is_valid(client_id, x_api_key):
|
| 122 |
res.status = HTTPStatus.FORBIDDEN
|
| 123 |
res.error = "Invalid credentials"
|
| 124 |
return res.json()
|
|
|
|
| 148 |
return res.json()
|
| 149 |
|
| 150 |
def is_valid(u, p):
|
| 151 |
+
return u == CLIENT_ID and p == X_API_KEY
|
| 152 |
|
| 153 |
if __name__=='__main__':
|
| 154 |
uvicorn.run('app:app', host='0.0.0.0', port=7860, reload=True)
|