Fixed first version of redirect and S3 policies

This commit is contained in:
2024-04-23 20:17:51 +02:00
parent fc03ef8ecd
commit 2cdc051191
2 changed files with 80 additions and 21 deletions

View File

@@ -1,21 +1,30 @@
import os
import boto3
import json
import boto3.exceptions
from botocore.exceptions import ClientError
s3_client = None
bucket_config = ''
bucket_data = ''
def lambda_handler(event: dict, context):
global s3_client
global s3_client, bucket_config, bucket_data
if s3_client is None:
print("Init Function")
bucket_config = os.environ.get('BUCKET_CONFIG', 'standout-config')
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()}")
try:
resp = s3_client.get_object(
Bucket='standout-data',
Bucket=bucket_config,
Key='redirects.json'
)
except s3_client.exceptions.NoSuchKey as e:
@@ -27,12 +36,12 @@ def lambda_handler(event: dict, context):
try:
redirects = json.load(resp["Body"])
params = event.get('queryStringParameters', {})
customer = redirects.get(params['cust_id'], {})
gadget = customer.get(params['gadget_id'], {})
customer = redirects.get(params['id'], {})
tag = customer.get(params['tag_id'], {})
content = gadget.get('content', None)
content = tag.get('content', None)
dest = None
if content and isinstance(content, dict):
if content and isinstance(content, dict) and not "type" in content.keys():
dest = content[params['face_id']]
else:
dest = content
@@ -41,9 +50,10 @@ def lambda_handler(event: dict, context):
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': 'standout-data',
'Key': dest['key']},
Params={'Bucket': bucket_data,
'Key': key},
ExpiresIn=120)
except ClientError as e:
print(e)
@@ -77,9 +87,9 @@ if __name__ == "__main__":
"path": "/",
"httpMethod": "GET",
"queryStringParameters": {
"cust_id": "cust_id1",
"gadget_id": "gadget_id1",
"face_id": "face_id3"
"id": "customer1",
"tag_id": "tag3",
"face_id": "face1"
},
}