Not able to get item from AWS dynamodb using python

0 votes

my python code

import boto3 
from boto3 import dynamodb 

from boto3.session import Session

from boto3.dynamodb.conditions import Key, Attr


dynamodb_session = Session(aws_access_key_id='XXXXXXXXXXXXXXX',
          aws_secret_access_key='XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
          region_name='us-east-1')

dynamodb = dynamodb_session.resource('dynamodb')

table=dynamodb.Table('Garbage_collector_table')

my_topic = "$aws/things/garbage_collector_thing/shadow/update/accepted"

response = table.get_item(TableName='Garbage_collector_table', Key={'topic':my_topic})

for res in response: 
    print "result ",res

I am getting the following error

Traceback (most recent call last):
 File "get-data-dynamodb-boto3.py", line 19, in <module>
    response = table.get_item(TableName='Garbage_collector_table', Key={'topic': my_topic})   File
 "/usr/local/lib/python2.7/dist-packages/boto3/resources/factory.py",
 line 518, in do_action
     response = action(self, *args, **kwargs)   File "/usr/local/lib/python2.7/dist-packages/boto3/resources/action.py",
 line 83, in __call__
     response = getattr(parent.meta.client, operation_name)(**params)   File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line
 258, in _api_call
     return self._make_api_call(operation_name, kwargs)   File /usr/local/lib/python2.7/dist-packages/botocore/client.py", line 548,
 in _make_api_call
     raise ClientError(parsed_response, operation_name)
Feb 24, 2022 in AWS by Rahul
• 2,120 points
1,713 views

1 answer to this question.

0 votes

You are mixing resource and client objects which have different methods.

The correct syntax for the resource is 

response= get_item(Key={'topic':my_topic})

but personally I recommend using boto client 

client= boto3.client('dynamodb')
response=client.get_item(TableName='garbage_collector_table', Key= {'topic':'S': str(my_topic)}})

answered Feb 24, 2022 by Korak
• 5,820 points

Related Questions In AWS

+2 votes
3 answers
+1 vote
3 answers

Not able to pass params POST to AWS Lambda from Amazon API Gateway

For this template : application/x-www-form-urlencoded  This line below will ...READ MORE

answered Jun 13, 2018 in AWS by Cloud gunner
• 4,670 points
8,744 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to set-up DynamoDB trigger using AWS Lambda?

Well this code worked for me. You ...READ MORE

answered Aug 20, 2018 in AWS by Archana
• 4,170 points
2,839 views
0 votes
1 answer
0 votes
1 answer

AWS DynamoDB Queries Not Showing user data unless with Admin privileges

Replace ownerfield "username" with "id"   type user @model @auth ...READ MORE

answered Feb 23, 2022 in AWS by Korak
• 5,820 points
474 views
0 votes
1 answer

How to change instance type in aws

As your needs change, you might find ...READ MORE

answered Feb 4, 2022 in AWS by Korak
• 5,820 points
496 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP