I am using Pydantic
to validate and type an incoming S3 Event
in an AWS Lambda
function.
The event looks like this (only including relevant bits):
{"Records": [ {"s3": {"bucket": {"name": "my-bucket" },"object": {"key": "MYKEY%28CSV%29/XXXX.CSV" } } } ]}
I define my Model
like this to get the relevant information.
from pydantic import BaseModelclass ObjectInfo(BaseModel): key: strclass BucketInfo(BaseModel): name: strclass S3Schema(BaseModel): bucket: BucketInfo object: ObjectInfoclass Record(BaseModel): s3: S3Schemaclass DeletionEvent(BaseModel): Records: list[Record]def handler(event: dict, _): eventTyped = DeletionEvent(**event) return True
Now the problem is that the correct value for key
is supposed to be MYKEY(CSV)/XXXX.CSV
, not MYKEY%28CSV%29/XXXX.CSV
. I usually fix this issue using urllib.parse.unquote_plus
to decode the %XX
bits representing special characters. I think I can define a custom decoder but this seems like overkill.
Is there any way to get pydantic
to do this decoding for me? It has a bunch of classes for working with URLs but I don't see anything about decoding URL encoded strings by themselves.