Skip to content

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

Closed

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

Activity

  1. added
    bug
    triage
    on Mar 29, 2021
  2. byron70 commented on Mar 30, 2021

    @byron70
    Author

    @byron70 Otherwise, is there a feature missing in the data for S3 Object?

    Best I can tell everything else seems to be in order.

  3. added this to the 1.14.0 milestone on Mar 30, 2021
  4. heitorlessa commented on Mar 30, 2021

    @heitorlessa
    Contributor

    Fix is in - @byron70 could you double check in case I've missed anything obvious? Had to upgrade Pydantic to 1.8 as this new type was introduced too.

    #375

    Once merged, this will make to the next release hopefully mid next week

  5. self-assigned this
    on Mar 30, 2021
  6. removed
    triage
    on Mar 30, 2021
  7. added
    pending-release
    on Apr 7, 2021
  8. heitorlessa commented on Apr 9, 2021

    @heitorlessa
    Contributor

    hey @byron70 this is now out as part of the 1.14.0 release

  9. removed
    pending-release
    on Jul 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions