Added layer, fixed add of content

This commit is contained in:
2024-05-01 12:00:48 +02:00
parent e149ab7c80
commit c2c268c0fd
4 changed files with 99 additions and 36 deletions

View File

@@ -47,14 +47,15 @@ def lambda_handler(event: dict, context):
## Download redirects file ## Download redirects file
redirects: Redirects redirects: Redirects
try: try:
#resp = s3_client.get_object( if context is not None:
# Bucket=bucket_config, resp = s3_client.get_object(
# Key='redirects.json' Bucket=bucket_config,
#) Key='redirects.json'
#redirects = Redirects(**json.load(resp['Body'])) )
with open('/home/emanuele/dev/StandOut/lambda_config/redirects.json', 'r') as f: redirects = Redirects.model_validate_json(resp['Body'].read())
#redirects = Redirects(**json.load(f)) else:
redirects = Redirects.model_validate_json(f.read()) with open('/home/emanuele/dev/StandOut/lambda_config/redirects.json', 'r') as f:
redirects = Redirects.model_validate_json(f.read())
except s3_client.exceptions.NoSuchKey as e: except s3_client.exceptions.NoSuchKey as e:
print(e) print(e)
# Oppure pagina "siamo spiacenti ma il contenuto non e' disponibile" # Oppure pagina "siamo spiacenti ma il contenuto non e' disponibile"
@@ -73,23 +74,38 @@ def lambda_handler(event: dict, context):
# splitta la chiave per capire la directory # splitta la chiave per capire la directory
keys = record.s3.object.key.split('/') keys = record.s3.object.key.split('/')
file_name = keys[-1] keys.reverse()
match record.eventName: match record.eventName:
case "ObjectCreated:Put" | "ObjectCreated:Post": case "ObjectCreated:Put" | "ObjectCreated:CompleteMultipartUpload":
print(f"ObjectCreated: {record.s3.object.key}") print(f"ObjectCreated: {record.s3.object.key}")
# crea il primo utente se necessario o selezionalo # crea il primo utente se necessario o selezionalo
cust_key = keys.pop()
if redirects.customers is None: if redirects.customers is None:
redirects.customers = {keys[0]: Customer(status='active')} redirects.customers = {cust_key: Customer(status="active")}
c = redirects.customers[keys[0]] if cust_key not in redirects.customers.keys():
redirects.customers[cust_key] = Customer(status="active")
# Aggiunto solo un cliente
if len(keys) == 0:
break
c = redirects.customers[cust_key]
# crea un tag per l'utente, con contenuto nullo o selezionalo # crea un tag per l'utente, con contenuto nullo o selezionalo
tag_key = keys.pop()
if c.tags is None: if c.tags is None:
c.tags = {keys[1]: Tag(status="active", content=None)} c.tags = {tag_key: Tag(status="active", content=None)}
t = c.tags[keys[1]] if tag_key not in c.tags.keys():
c.tags[tag_key] = Tag(status="active", content=None)
# Aggiunta anche una chiave
if len(keys) == 0:
break
t = c.tags[tag_key]
# crea un contenuto per il tag a seconda della lunghezza della chiave # crea un contenuto per il tag a seconda della lunghezza della chiave
file_name = keys[0]
if file_name == "url.txt": if file_name == "url.txt":
with s3_client.get_object(Bucket=bucket_data, Key=record.s3.object.key)['Body'] as url_file: with s3_client.get_object(Bucket=bucket_data, Key=record.s3.object.key)['Body'] as url_file:
content = Content(type='url', key=file_name, url=url_file.readline().decode().strip()) content = Content(type='url', key=file_name, url=url_file.readline().decode().strip())
@@ -97,33 +113,35 @@ def lambda_handler(event: dict, context):
content = Content(type='s3', key=file_name, url=None) content = Content(type='s3', key=file_name, url=None)
match len(keys): match len(keys):
case 4: case 2:
if t.content is None: if t.content is None or isinstance(t.content, Content):
t.content = {keys[2]: content} t.content = {keys[1]: content}
elif isinstance(t.content, dict): elif isinstance(t.content, dict):
t.content[keys[2]] = content t.content[keys[1]] = content
case 3: case 1:
t.content = content t.content = content
case _: case _:
print("Too long keys") print("Too long keys")
with open('/home/emanuele/dev/StandOut/lambda_config/redirects.json', 'w') as f:
f.write(redirects.model_dump_json(indent=2))
return True
case "ObjectCreated:Copy": case "ObjectCreated:Copy":
print(f"Object copy: {record.s3.object.key}") print(f"Object copy: {record.s3.object.key}")
return True
case "s3:ObjectRemoved:Delete":
case "s3:ObjectRemoved:*":
print(f"Object remove: {record.s3.object.key}") print(f"Object remove: {record.s3.object.key}")
return True
case _: case _:
print("Unknown action") print("Unknown action")
if context is not None:
resp = s3_client.put_object(Bucket=bucket_config,
Key='redirects.json',
Body=redirects.model_dump_json(indent=2))
print(f"New redirects version: {resp['ETag']}")
else:
with open('/home/emanuele/dev/StandOut/lambda_config/redirects.json', 'w') as f:
f.write(redirects.model_dump_json(indent=2))
return True
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -1,17 +1,33 @@
{ {
"customers": { "customers": {
"customer1": { "customer1": {
"status": "active",
"tags": {
"tag1": {
"status": "active",
"content": {
"type": "url",
"key": "url.txt",
"url": "https://grafana.etss.it/d/LbON5PkGz/power?orgId=1&from=now-12h&to=now&refresh=30s"
}
},
"tag2": {
"status": "active",
"content": {
"type": "s3",
"key": "file.txt",
"url": null
}
}
}
},
"customer2": {
"status": "active", "status": "active",
"tags": { "tags": {
"tag1": { "tag1": {
"status": "active", "status": "active",
"content": { "content": {
"face1": { "face1": {
"type": "url",
"key": "url.txt",
"url": "https://grafana.etss.it/d/LbON5PkGz/power?orgId=1&from=now-12h&to=now&refresh=30s"
},
"face2": {
"type": "s3", "type": "s3",
"key": "file.txt", "key": "file.txt",
"url": null "url": null
@@ -19,6 +35,19 @@
} }
} }
} }
},
"customer3": {
"status": "active",
"tags": {
"tag1": {
"status": "active",
"content": {
"type": "s3",
"key": "VID20240116160134.mp4",
"url": null
}
}
}
} }
} }
} }

View File

@@ -27,7 +27,7 @@
"arn": "arn:aws:s3:::standout-data" "arn": "arn:aws:s3:::standout-data"
}, },
"object": { "object": {
"key": "customer1/tag1/face2/file.txt", "key": "customer1/tag2/file.txt",
"size": 2844326, "size": 2844326,
"eTag": "7039e5338840f289d0510dc9149bf0b5", "eTag": "7039e5338840f289d0510dc9149bf0b5",
"sequencer": "00662A206F99CD2E09" "sequencer": "00662A206F99CD2E09"

View File

@@ -150,6 +150,18 @@ data "archive_file" "lambda_standout_config_code" {
output_path = "./lambda_zip/standout_lambda_config.zip" output_path = "./lambda_zip/standout_lambda_config.zip"
} }
data "archive_file" "lambda_layer_deps" {
type = "zip"
source_dir = "./lambda_layer"
output_path = "./lambda_zip/lambda_layer.zip"
}
resource "aws_lambda_layer_version" "lambda_layer" {
filename = "./lambda_zip/lambda_layer.zip"
layer_name = "lambda_deps"
compatible_runtimes = ["python3.12"]
}
resource "aws_lambda_function" "lambda_standout_redirect" { resource "aws_lambda_function" "lambda_standout_redirect" {
# If the file is not in the current working directory you will need to include a # If the file is not in the current working directory you will need to include a
# path.module in the filename. # path.module in the filename.
@@ -162,6 +174,8 @@ resource "aws_lambda_function" "lambda_standout_redirect" {
runtime = "python3.12" runtime = "python3.12"
layers = [aws_lambda_layer_version.lambda_layer.arn]
timeout = 10 timeout = 10
environment { environment {
@@ -184,6 +198,8 @@ resource "aws_lambda_function" "lambda_standout_config" {
runtime = "python3.12" runtime = "python3.12"
layers = [aws_lambda_layer_version.lambda_layer.arn]
timeout = 10 timeout = 10
environment { environment {