Skip to content

parser.models.S3Object should allow size of >= 0 #372

Closed
@byron70

Description

@byron70

Attempting to parse a S3 PUT event in an AWS Lambda where the S3 object size is 0 bytes, yields a pydantic.error_wrappers.ValidationError indicating size must be greater than 0.

Expected Behavior

Since 0 byte objects in S3 are allowed, it is expected these types of S3 events would be validated without issue when parsing the Lambda event.

Current Behavior

Receiving the following error attempting to parse a 0 byte S3 PUT event.

parse(event=event, model=models.S3Model, envelope=envelopes.SnsSqsEnvelope)

pydantic.error_wrappers.ValidationError: 1 validation error for S3Model
Records -> 0 -> s3 -> object -> size
  ensure this value is greater than 0 (type=value_error.number.not_gt; limit_value=0)

Possible Solution

Change S3Object to the following:

class S3Object(BaseModel):
    key: str
    size: NonNegativeInt
    eTag: str
    sequencer: str
    versionId: Optional[str]

Steps to Reproduce (for bugs)

from aws_lambda_powertools.utilities.parser import parse, models, envelopes

event = {
    "Records": [
        {
            "eventVersion": "2.1",
            "eventSource": "aws:s3",
            "awsRegion": "us-east-2",
            "eventTime": "2021-03-25T22:41:47.873Z",
            "eventName": "ObjectCreated:Put",
            "userIdentity": {"principalId": "AWS:ARODJFUCGRB5ZBMY74FY:foo.bar"},
            "requestParameters": {"sourceIPAddress": "127.0.0.1"},
            "responseElements": {
                "x-amz-request-id": "foo",
                "x-amz-id-2": "foo",
            },
            "s3": {
                "s3SchemaVersion": "1.0",
                "configurationId": "tf-s3-topic-cur",
                "bucket": {
                    "name": "bucket",
                    "ownerIdentity": {"principalId": "ME"},
                    "arn": "arn:aws:s3:::bucket",
                },
                "object": {
                    "key": "reports/test/test.json",
                    "size": 0,
                    "eTag": "foo",
                    "versionId": "foo",
                    "sequencer": "foo",
                },
            },
        }
    ]
}
parse(event=event, model=models.S3Model)

Environment

aws-lambda-powertools==1.13.0
python 3.8

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Triage

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions