简体   繁体   中英

Python Boto3 AWS Lambda TypeDeserializer - Error: TypeError: Dynamodb type <name> is not supported

This is the revelant part of my python lambda function:

def retrivearticle(id):
"""function that return a specific article"""
data = client.get_item(
    TableName=os.environ.get("STORAGE_GIODEMWEBSITE_NAME"),
    Key={
        'id': {
            'S': id
        }
    }
)
data = fromLowLevelToPython(data)
return data

where fromLowLevelToPython is:

def fromLowLevelToPython(data):
    """Convert low-leve dynamodb response into regular json"""
    deserializer = TypeDeserializer()
    python_data = {k: deserializer.deserialize(v) for k, v in data.items()}
    return python_data

then I have this item in my dynamo DB table: enter image description here enter image description here

    {
  "id": {
    "S": "test"
  },
  "value": {
    "S": "test"
  }
}

When I run my query I get this error

    [ERROR] TypeError: Dynamodb type id is not supported
Traceback (most recent call last):
  File "/var/task/index.py", line 21, in handler
    response = retrivearticle(id)
  File "/var/task/index.py", line 54, in retrivearticle
    data = fromLowLevelToPython(data)
  File "/var/task/index.py", line 70, in fromLowLevelToPython
    python_data = {k: deserializer.deserialize(v) for k, v in data.items()}
  File "/var/task/index.py", line 70, in <dictcomp>
    python_data = {k: deserializer.deserialize(v) for k, v in data.items()}
  File "/var/task/boto3/dynamodb/types.py", line 269, in deserialize
    raise TypeError(

Do you know what is the problem here?

If I try locally it seems to works correctly:

In [13]: import boto3

In [14]: from boto3.dynamodb.types import TypeDeserializer

In [15]: ll = {
    ...:   "id": {
    ...:     "S": "test"
    ...:   },
    ...:   "value": {
    ...:     "S": "test"
    ...:   }
    ...: }

In [16]: def fromLowLevelToPython(data):
    ...:     """Convert low-leve dynamodb response into regular json"""
    ...:     deserializer = TypeDeserializer()
    ...:     python_data = {k: deserializer.deserialize(v) for k, v in data.items()}
    ...:     return python_data
    ...: 

In [17]: fromLowLevelToPython(ll)
Out[17]: {'id': 'test', 'value': 'test'}

(Translated using Hero Translate )

I was trying to get this to work with paginate. Here's how to do it:

from boto3.dynamodb.types import TypeDeserializer

client = boto3.client('dynamodb', region_name='eu-west-1')
paginator = client.get_paginator('scan').paginate(
    TableName='TABLE NAME',
    PaginationConfig={
        'MaxItems': 123,
        'PageSize': 123
    })

deserializer = TypeDeserializer()
for page in paginator:
      for item in page['Items']:
          python_data = deserializer.deserialize({'M':item})
          print(python_data)

The deserialize line used here should fix your original issue like so: deserializer.deserialize({'M':data})

Basically it thinks that id is a type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM