Getting an item from DynamoDB table

0

I am trying to get an attribute from a table in DynamoDB. I can see all items in the table via a print and see the attributes in the list. However, when I try to get just that attribute, I get a response of "none".

The example code was attained from: https://medium.com/@ramjoshi.blogs/aws-dynamodb-with-python-a-beginners-guide-b9cf101436b2

I am executing from Lambda. Here's the code: import json import boto3 from boto3.dynamodb.conditions import Key

def lambda_handler(event, context): dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('D1-Sites')

response = table.get_item(Key={'SitesID': '1'})
print(response)

item = response.get('D1-Website')

print("Should be printing item")
print(item)

return {
    'statusCode': 200,
    'body': json.dumps('Hello from Lambda!')
}

In the 1st print, I see a list of every attribute in record 1. In the last one, it prints "None"

I would like to see the value for a specific attribute: D1-Website.

Any help is appreciated.

profile picture
Petrus
gefragt vor 2 Monaten246 Aufrufe
3 Antworten
0

Not out of the woods yet: Here's the code now:

import json import boto3 from boto3.dynamodb.conditions import Key

def lambda_handler(event, context):

dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table('D1-Sites')  

response = table.get_item(Key={'SitesID': '1'})
print ('response: ')
print(response)

# from re:Post
item = response.get('D1-Website', {'SitesID': '1'}) # Provide a default value to handle
d1_website = item.get('D1-Website')

print('d1_website is:')
print(d1_website)#print the URL

return {
    'statusCode': 200,
    'body': json.dumps('Hello from Lambda!')
}

And the response: response: {'Item': {'D1-Pricing': 'https://pricingdayones.godaddysites.com/', 'D1-QnA': 'https://faqdayoneslive.godaddysites.com/', 'SitesID': '1', 'D1-Youtube': 'https://m.youtube.com/@DayOnes_live', 'D1-ArtistQuickGuide': 'artistguide.dayones.live', 'D1-FanQuickGuide': 'fanguide.dayones.live', 'D1-Website': 'http://dayones.live'},

d1_website is: None

Printing the response from "table.get_item(Key={'SitesID': '1'})" shows indeed a list of the table variables for SitesID = 1....the Partition Key. "D1-Website" is the last return from the attributes in the table with the value.

However, I am still getting a none in variable d1_website attempting ti get the data. What have I not done correctly?

Peter

profile picture
Petrus
beantwortet vor 2 Monaten
  • Is none because you have to get the “Item” field: response[“Item”][“D1-Website”]

0

Hello,

response.get('D1-Website') tries to retrieve the value of the 'D1-Website' key directly from the response dictionary. However, since the response dictionary doesn't have a top-level key named 'D1-Website', this operation returns None. Instead, you should first extract the item from the response and then access the 'D1-Website' attribute from the item

response = table.get_item(Key={'SitesID': '1'}) item = response.get('Item', {}) # Provide a default value to handle None response d1_website = item.get('D1-Website')

Paul
beantwortet vor 2 Monaten
profile picture
EXPERTE
überprüft vor 2 Monaten
0

As reported here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.ReadItem.html you may want to retrieve the item via response["Item"] first, and then by the field.

profile picture
EXPERTE
beantwortet vor 2 Monaten
profile picture
EXPERTE
überprüft vor 2 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen