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
redirects: Redirects
try:
#resp = s3_client.get_object(
# 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(**json.load(f))
redirects = Redirects.model_validate_json(f.read())
if context is not None:
resp = s3_client.get_object(
Bucket=bucket_config,
Key='redirects.json'
)
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"
@@ -73,23 +74,38 @@ def lambda_handler(event: dict, context):
# splitta la chiave per capire la directory
keys = record.s3.object.key.split('/')
file_name = keys[-1]
keys.reverse()
match record.eventName:
case "ObjectCreated:Put" | "ObjectCreated:Post":
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 = {keys[0]: Customer(status='active')}
c = redirects.customers[keys[0]]
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 = {keys[1]: Tag(status="active", content=None)}
t = c.tags[keys[1]]
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())
@@ -97,33 +113,35 @@ def lambda_handler(event: dict, context):
content = Content(type='s3', key=file_name, url=None)
match len(keys):
case 4:
if t.content is None:
t.content = {keys[2]: content}
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[2]] = content
case 3:
t.content[keys[1]] = content
case 1:
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
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__":

View File

@@ -1,17 +1,33 @@
{
"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": "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
@@ -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"
},
"object": {
"key": "customer1/tag1/face2/file.txt",
"key": "customer1/tag2/file.txt",
"size": 2844326,
"eTag": "7039e5338840f289d0510dc9149bf0b5",
"sequencer": "00662A206F99CD2E09"