Fixed multi face content add, skips adding folders
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -1,17 +1,38 @@
|
||||
{
|
||||
"customers": {
|
||||
"customer3": {
|
||||
"8eb763ceacec418cb48c81dae40bcde0": {
|
||||
"status": "active",
|
||||
"name": "cliente-test",
|
||||
"tags": {
|
||||
"tag1": {
|
||||
"status": "active",
|
||||
"content": {
|
||||
"type": "url",
|
||||
"key": "url.txt",
|
||||
"url": "https://www.instagram.com/lisavarano?igsh=M2V6ZHhqMG91cmF1&utm_source=qr",
|
||||
"tag_url": "https://sel2p8wy6c.execute-api.eu-west-1.amazonaws.com/dev/api?id=8eb763ceacec418cb48c81dae40bcde0&tag_id=tag1"
|
||||
}
|
||||
},
|
||||
"tag2": {
|
||||
"status": "active",
|
||||
"content": {
|
||||
"type": "s3",
|
||||
"key": "foo.txt",
|
||||
"url": null
|
||||
"key": "f3",
|
||||
"url": null,
|
||||
"tag_url": "https://api.standout.it/dev/api?id=8eb763ceacec418cb48c81dae40bcde0&tag_id=tag2"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"0ef1d0acb1bf4ebab77f5a8c9c56d6b5": {
|
||||
"status": "active",
|
||||
"name": "foo",
|
||||
"tags": {
|
||||
"help": {
|
||||
"status": "active",
|
||||
"content": null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,8 +27,8 @@
|
||||
"arn": "arn:aws:s3:::standout-data"
|
||||
},
|
||||
"object": {
|
||||
"key": "customer3/tag2/foo.txt",
|
||||
"size": 2844326,
|
||||
"key": "cliente-test/tag2/f3/",
|
||||
"size": 0,
|
||||
"eTag": "7039e5338840f289d0510dc9149bf0b5",
|
||||
"sequencer": "00662A206F99CD2E09"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user