Added lambda auto config
This commit is contained in:
@@ -2,12 +2,23 @@ import os
|
||||
import boto3
|
||||
import json
|
||||
import boto3.exceptions
|
||||
from botocore.exceptions import ClientError
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
|
||||
s3_client = None
|
||||
bucket_config = ''
|
||||
bucket_data = ''
|
||||
|
||||
class RequestParams(BaseModel):
|
||||
id: str
|
||||
tag_id: str
|
||||
face_id: Optional[str] = None
|
||||
|
||||
class Content(BaseModel):
|
||||
type: str
|
||||
key: str
|
||||
url: Optional[str] = None
|
||||
|
||||
def lambda_handler(event: dict, context):
|
||||
global s3_client, bucket_config, bucket_data
|
||||
|
||||
@@ -17,11 +28,9 @@ def lambda_handler(event: dict, context):
|
||||
bucket_data = os.environ.get('BUCKET_DATA', 'standout-data')
|
||||
print(f'Bucket Config: {bucket_config}')
|
||||
print(f' Bucket Data: {bucket_data}')
|
||||
|
||||
s3_client = boto3.client('s3')
|
||||
for x in s3_client.list_buckets()['Buckets']:
|
||||
print(f"{x['Name']}: {x['CreationDate'].isoformat()}")
|
||||
|
||||
## Download redirects file
|
||||
try:
|
||||
resp = s3_client.get_object(
|
||||
Bucket=bucket_config,
|
||||
@@ -33,32 +42,34 @@ def lambda_handler(event: dict, context):
|
||||
return {
|
||||
"statusCode": 404
|
||||
}
|
||||
|
||||
## Parse request and get content
|
||||
try:
|
||||
redirects = json.load(resp["Body"])
|
||||
params = event.get('queryStringParameters', {})
|
||||
customer = redirects.get(params['id'], {})
|
||||
tag = customer.get(params['tag_id'], {})
|
||||
redirects = json.load(resp['Body'])
|
||||
|
||||
params = RequestParams(**event.get('queryStringParameters', {}))
|
||||
customer = redirects.get(params.id, {})
|
||||
tag = customer.get(params.tag_id, {})
|
||||
content = tag.get('content', None)
|
||||
dest = None
|
||||
if content and isinstance(content, dict) and not "type" in content.keys():
|
||||
dest = content[params['face_id']]
|
||||
else:
|
||||
dest = content
|
||||
|
||||
if dest and isinstance(dest, dict):
|
||||
match dest.get('type', 's3'):
|
||||
case "s3":
|
||||
try:
|
||||
key = f'{params['id']}/{params['tag_id']}/{dest['key']}'
|
||||
response = s3_client.generate_presigned_url('get_object',
|
||||
Params={'Bucket': bucket_data,
|
||||
'Key': key},
|
||||
ExpiresIn=120)
|
||||
except ClientError as e:
|
||||
print(e)
|
||||
finally:
|
||||
dest = response
|
||||
|
||||
# In case of multi face tag select the correct face
|
||||
if isinstance(content, dict) and params.face_id:
|
||||
content = content.get(params.face_id, None)
|
||||
if content is None:
|
||||
return {
|
||||
"statusCode": 404
|
||||
}
|
||||
|
||||
content = Content(**content)
|
||||
match content.type:
|
||||
case "s3":
|
||||
key = f'{params.id}/{params.tag_id}/{content.key}'
|
||||
final_redirect = s3_client.generate_presigned_url('get_object',
|
||||
Params={'Bucket': bucket_data,
|
||||
'Key': key},
|
||||
ExpiresIn=120)
|
||||
case "url":
|
||||
final_redirect = content.url
|
||||
|
||||
|
||||
except json.decoder.JSONDecodeError as je:
|
||||
@@ -68,6 +79,11 @@ def lambda_handler(event: dict, context):
|
||||
}
|
||||
except KeyError as ke:
|
||||
print(ke)
|
||||
return {
|
||||
"statusCode": 404
|
||||
}
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return {
|
||||
"statusCode": 500
|
||||
}
|
||||
@@ -76,7 +92,7 @@ def lambda_handler(event: dict, context):
|
||||
"statusCode": 301,
|
||||
"headers": {
|
||||
"Cache-Control": "no-cache",
|
||||
"Location": str(dest)
|
||||
"Location": str(final_redirect)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user