Compare commits

...

2 Commits

Author SHA1 Message Date
c2c268c0fd Added layer, fixed add of content 2024-05-01 12:00:48 +02:00
e149ab7c80 First version of lambda config put object 2024-05-01 10:42:56 +02:00
5 changed files with 186 additions and 22 deletions

View File

@@ -3,7 +3,8 @@ import json
import boto3
from typing import Optional
from pydantic import BaseModel
from redirects_base import Redirects
import pydantic_core
from redirects_base import Content, Customer, Redirects, Tag
s3_client = None
bucket_config = ''
@@ -44,13 +45,17 @@ def lambda_handler(event: dict, context):
s3_client = boto3.client('s3')
## Download redirects file
redirects = None
redirects: Redirects
try:
if context is not None:
resp = s3_client.get_object(
Bucket=bucket_config,
Key='redirects.json'
)
redirects = Redirects(**json.load(resp['Body']))
redirects = Redirects.model_validate_json(resp['Body'].read())
else:
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:
print(e)
# Oppure pagina "siamo spiacenti ma il contenuto non e' disponibile"
@@ -67,26 +72,78 @@ def lambda_handler(event: dict, context):
print(f"Action: {record.eventName}")
print(f"Object: {record.s3}")
match record.eventName:
case "ObjectCreated:Put" | "ObjectCreated:Post":
print(f"Object add: {record.s3.object.key}")
key_components = record.s3.object.key.split('/')
# capire il numero di key components, aggiornare il modello
return True
# splitta la chiave per capire la directory
keys = record.s3.object.key.split('/')
keys.reverse()
match record.eventName:
case "ObjectCreated:Put" | "ObjectCreated:CompleteMultipartUpload":
print(f"ObjectCreated: {record.s3.object.key}")
# crea il primo utente se necessario o selezionalo
cust_key = keys.pop()
if redirects.customers is None:
redirects.customers = {cust_key: Customer(status="active")}
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
tag_key = keys.pop()
if c.tags is None:
c.tags = {tag_key: Tag(status="active", content=None)}
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
file_name = keys[0]
if file_name == "url.txt":
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())
else:
content = Content(type='s3', key=file_name, url=None)
match len(keys):
case 2:
if t.content is None or isinstance(t.content, Content):
t.content = {keys[1]: content}
elif isinstance(t.content, dict):
t.content[keys[1]] = content
case 1:
t.content = content
case _:
print("Too long keys")
case "ObjectCreated:Copy":
print(f"Object copy: {record.s3.object.key}")
return True
case "s3:ObjectRemoved:*":
case "s3:ObjectRemoved:Delete":
print(f"Object remove: {record.s3.object.key}")
return True
case _:
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__":
lambda_handler({}, None)
with open('/home/emanuele/dev/StandOut/lambda_config/test.json', 'r') as f:
lambda_handler(json.load(f), None)

View File

@@ -0,0 +1,53 @@
{
"customers": {
"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",
"tags": {
"tag1": {
"status": "active",
"content": {
"face1": {
"type": "s3",
"key": "file.txt",
"url": null
}
}
}
}
},
"customer3": {
"status": "active",
"tags": {
"tag1": {
"status": "active",
"content": {
"type": "s3",
"key": "VID20240116160134.mp4",
"url": null
}
}
}
}
}
}

View File

@@ -4,7 +4,7 @@ from pydantic import BaseModel
class Content(BaseModel):
type: str
key: str
url: Optional[str | None]
url: Optional[str | None] = None
class Tag(BaseModel):
status: str
@@ -15,7 +15,7 @@ class Customer(BaseModel):
tags: Dict[str, Tag] | None = None
class Redirects(BaseModel):
customers: Dict[str, Customer]
customers: Dict[str, Customer] | None = None
if __name__ == "__main__":
r = Redirects (

38
lambda_config/test.json Normal file
View File

@@ -0,0 +1,38 @@
{
"Records": [
{
"eventVersion": "2.1",
"eventSource": "aws:s3",
"awsRegion": "eu-west-1",
"eventTime": "2024-04-25T09:20:47.735Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "AWS:AIDA4HZZTH2GIL7SGDHDP"
},
"requestParameters": {
"sourceIPAddress": "93.44.141.63"
},
"responseElements": {
"x-amz-request-id": "PKAA3E505JRZ6RD5",
"x-amz-id-2": "JS2xXacUSZPbV5zC73sizKo5A0Am1qxadYy+KiAN4j5qSC89U5HueSXWMUWjHDdPryNBntkthWxfnKZSRznWucX0knBUkUfx"
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "tf-s3-lambda-20240425091712738000000001",
"bucket": {
"name": "standout-data",
"ownerIdentity": {
"principalId": "A37RIV9ZNURQAX"
},
"arn": "arn:aws:s3:::standout-data"
},
"object": {
"key": "customer1/tag2/file.txt",
"size": 2844326,
"eTag": "7039e5338840f289d0510dc9149bf0b5",
"sequencer": "00662A206F99CD2E09"
}
}
}
]
}

View File

@@ -150,6 +150,18 @@ data "archive_file" "lambda_standout_config_code" {
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" {
# If the file is not in the current working directory you will need to include a
# path.module in the filename.
@@ -162,6 +174,8 @@ resource "aws_lambda_function" "lambda_standout_redirect" {
runtime = "python3.12"
layers = [aws_lambda_layer_version.lambda_layer.arn]
timeout = 10
environment {
@@ -184,6 +198,8 @@ resource "aws_lambda_function" "lambda_standout_config" {
runtime = "python3.12"
layers = [aws_lambda_layer_version.lambda_layer.arn]
timeout = 10
environment {