Fixed multi face content add, skips adding folders

This commit is contained in:
2024-05-26 10:44:12 +02:00
parent b1b03e8d07
commit d2b05f72a6
9 changed files with 191 additions and 149 deletions

View File

@@ -6,10 +6,11 @@ from pydantic import BaseModel
from typing import Dict, Optional
from redirects_base import Content, Customer, Redirects, Tag
s3_client = None
bucket_config = ''
bucket_data = ''
function_url = ''
s3_client = None
redirects: Redirects | None = None
class S3Bucket(BaseModel):
name: str
@@ -34,7 +35,7 @@ class Record(BaseModel):
s3: S3Event
def lambda_handler(event: dict, context):
global s3_client, bucket_config, bucket_data, function_url
global s3_client, bucket_config, bucket_data, function_url, redirects
if s3_client is None:
print("Init Function")
@@ -45,33 +46,28 @@ def lambda_handler(event: dict, context):
print(f' Bucket Data: {bucket_data}')
s3_client = boto3.client('s3')
## Download redirects file
redirects: Redirects
try:
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(), strict=False)
except s3_client.exceptions.NoSuchKey as e:
print(e)
# Oppure pagina "siamo spiacenti ma il contenuto non e' disponibile"
return {
"statusCode": 404
}
# Proces records
### Process records
for r in event["Records"]:
record = Record(**r)
if record.eventSource != "aws:s3":
return False
## Stampa info di debug
print(f"Action: {record.eventName}")
print(f"Object: {record.s3}")
print(f"Object: {record.s3.object}")
## Ritorna se la chiave non e' un file
if record.s3.object.key.endswith('/') and record.s3.object.size == 0:
print(f"Skip, folder only: {record.s3.object.key}")
continue
## Scarica il redirects solo il primo giro
if redirects is None:
try:
redirects = downloadRedirects(client = s3_client, context = context)
except s3_client.exceptions.NoSuchKey as e:
print(e)
return False
match record.eventName:
case "ObjectCreated:Put" | "ObjectCreated:CompleteMultipartUpload":
@@ -86,16 +82,30 @@ def lambda_handler(event: dict, context):
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']}")
if redirects is not None:
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))
else:
with open('/home/emanuele/dev/StandOut/lambda_config/redirects.json', 'w') as f:
f.write(redirects.model_dump_json(indent=2))
print("No Action")
return True
def downloadRedirects(client, context) -> Redirects:
if context is not None:
resp = client.get_object(
Bucket=bucket_config,
Key='redirects.json'
)
return Redirects.model_validate_json(resp['Body'].read())
else:
with open('/home/emanuele/dev/StandOut/lambda_config/redirects.json', 'r') as f:
return Redirects.model_validate_json(f.read(), strict=False)
def getObjectKeys(record: Record) -> list[str]:
keys = [v for v in record.s3.object.key.split('/') if v != '']
@@ -206,7 +216,7 @@ def processDelete(record: Record, redirects: Redirects) -> None:
print("Unexpected")
return
print(f"Object remove: {record.s3.object.key}")
print(f"Object Remove: {record.s3.object.key}")
if __name__ == "__main__":
with open('/home/emanuele/dev/StandOut/lambda_config/test.json', 'r') as f: