First version of lambda config put object
This commit is contained in:
@@ -3,7 +3,8 @@ import json
|
|||||||
import boto3
|
import boto3
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from redirects_base import Redirects
|
import pydantic_core
|
||||||
|
from redirects_base import Content, Customer, Redirects, Tag
|
||||||
|
|
||||||
s3_client = None
|
s3_client = None
|
||||||
bucket_config = ''
|
bucket_config = ''
|
||||||
@@ -44,13 +45,16 @@ def lambda_handler(event: dict, context):
|
|||||||
s3_client = boto3.client('s3')
|
s3_client = boto3.client('s3')
|
||||||
|
|
||||||
## Download redirects file
|
## Download redirects file
|
||||||
redirects = None
|
redirects: Redirects
|
||||||
try:
|
try:
|
||||||
resp = s3_client.get_object(
|
#resp = s3_client.get_object(
|
||||||
Bucket=bucket_config,
|
# Bucket=bucket_config,
|
||||||
Key='redirects.json'
|
# Key='redirects.json'
|
||||||
)
|
#)
|
||||||
redirects = Redirects(**json.load(resp['Body']))
|
#redirects = Redirects(**json.load(resp['Body']))
|
||||||
|
with open('/home/emanuele/dev/StandOut/lambda_config/redirects.json', 'r') as f:
|
||||||
|
#redirects = Redirects(**json.load(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"
|
||||||
@@ -67,11 +71,45 @@ def lambda_handler(event: dict, context):
|
|||||||
print(f"Action: {record.eventName}")
|
print(f"Action: {record.eventName}")
|
||||||
print(f"Object: {record.s3}")
|
print(f"Object: {record.s3}")
|
||||||
|
|
||||||
|
# splitta la chiave per capire la directory
|
||||||
|
keys = record.s3.object.key.split('/')
|
||||||
|
file_name = keys[-1]
|
||||||
|
|
||||||
match record.eventName:
|
match record.eventName:
|
||||||
case "ObjectCreated:Put" | "ObjectCreated:Post":
|
case "ObjectCreated:Put" | "ObjectCreated:Post":
|
||||||
print(f"Object add: {record.s3.object.key}")
|
print(f"ObjectCreated: {record.s3.object.key}")
|
||||||
key_components = record.s3.object.key.split('/')
|
|
||||||
# capire il numero di key components, aggiornare il modello
|
# crea il primo utente se necessario o selezionalo
|
||||||
|
if redirects.customers is None:
|
||||||
|
redirects.customers = {keys[0]: Customer(status='active')}
|
||||||
|
c = redirects.customers[keys[0]]
|
||||||
|
|
||||||
|
# crea un tag per l'utente, con contenuto nullo o selezionalo
|
||||||
|
if c.tags is None:
|
||||||
|
c.tags = {keys[1]: Tag(status="active", content=None)}
|
||||||
|
t = c.tags[keys[1]]
|
||||||
|
|
||||||
|
# crea un contenuto per il tag a seconda della lunghezza della chiave
|
||||||
|
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 4:
|
||||||
|
if t.content is None:
|
||||||
|
t.content = {keys[2]: content}
|
||||||
|
elif isinstance(t.content, dict):
|
||||||
|
t.content[keys[2]] = content
|
||||||
|
case 3:
|
||||||
|
t.content = content
|
||||||
|
case _:
|
||||||
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
@@ -89,4 +127,5 @@ def lambda_handler(event: dict, context):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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)
|
||||||
24
lambda_config/redirects.json
Normal file
24
lambda_config/redirects.json
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"customers": {
|
||||||
|
"customer1": {
|
||||||
|
"status": "active",
|
||||||
|
"tags": {
|
||||||
|
"tag1": {
|
||||||
|
"status": "active",
|
||||||
|
"content": {
|
||||||
|
"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",
|
||||||
|
"key": "file.txt",
|
||||||
|
"url": null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ from pydantic import BaseModel
|
|||||||
class Content(BaseModel):
|
class Content(BaseModel):
|
||||||
type: str
|
type: str
|
||||||
key: str
|
key: str
|
||||||
url: Optional[str | None]
|
url: Optional[str | None] = None
|
||||||
|
|
||||||
class Tag(BaseModel):
|
class Tag(BaseModel):
|
||||||
status: str
|
status: str
|
||||||
@@ -15,7 +15,7 @@ class Customer(BaseModel):
|
|||||||
tags: Dict[str, Tag] | None = None
|
tags: Dict[str, Tag] | None = None
|
||||||
|
|
||||||
class Redirects(BaseModel):
|
class Redirects(BaseModel):
|
||||||
customers: Dict[str, Customer]
|
customers: Dict[str, Customer] | None = None
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
r = Redirects (
|
r = Redirects (
|
||||||
|
|||||||
38
lambda_config/test.json
Normal file
38
lambda_config/test.json
Normal 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/tag1/face2/file.txt",
|
||||||
|
"size": 2844326,
|
||||||
|
"eTag": "7039e5338840f289d0510dc9149bf0b5",
|
||||||
|
"sequencer": "00662A206F99CD2E09"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user