英文:
How to get the if statement right for boto3
问题
我创建了一个包含两个记录的DynamoDB表。一个记录是RDS的ARN,另一个是日期字段名"last_reported"。现在,我正在尝试运行一个Lambda函数,该函数应该获取该记录。如果表中有数据,它应该执行表/项目的值,如果表中没有数据,它应该退出并显示一条消息。我收到以下错误消息。
{
"errorMessage": "'Item'",
"errorType": "KeyError",
"requestId": "",
"stackTrace": [
" File \"/var/lang/lib/python3.9/importlib/__init__.py\", line 127, in import_module",
" return _bootstrap._gcd_import(name[level:], package, level)",
" File \"<frozen importlib._bootstrap>\", line 1030, in _gcd_import",
" File \"<frozen importlib._bootstrap>\", line 1007, in _find_and_load",
" File \"<frozen importlib._bootstrap>\", line 986, in _find_and_load_unlocked",
" File \"<frozen importlib._bootstrap>\", line 680, in _load_unlocked",
" File \"<frozen importlib._bootstrap_external>\", line 850, in exec_module",
" File \"<frozen importlib._bootstrap>\", line 228, in _call_with_frames_removed",
" File \"/var/task/lambda_function.py\", line 339, in <module>",
" lambda_handler(1,1)",
" File \"/var/task/lambda_function.py\", line 263, in lambda_handler",
" last_reported = int(read_db['Item']['last_reported']['N'])"
]
}
DynamoDB表名为'my_table',键为'arn'。我使用boto3客户端连接到DynamoDB,并尝试执行查询操作,查找ARN等于给定值的记录。如果响应为True,我尝试获取项目并提取'last_reported'字段的值,然后打印它。
英文:
I created a dynamodb table with two records in there. One is the ARN of the rds and one is date field name last_reported. Now i am trying to run lambda function which should fetch that record. If there is data in the table it should execute the value of the table / item, if there is no data in the table, it should exit with a message. I get the following error
{
"errorMessage": "'Item'",
"errorType": "KeyError",
"requestId": "",
"stackTrace": [
" File \"/var/lang/lib/python3.9/importlib/__init__.py\", line 127, in import_module\n
return _bootstrap._gcd_import(name[level:], package, level)\n",
" File \"<frozen importlib._bootstrap>\", line 1030, in _gcd_import\n",
" File \"<frozen importlib._bootstrap>\", line 1007, in _find_and_load\n",
" File \"<frozen importlib._bootstrap>\", line 986, in _find_and_load_unlocked\n",
" File \"<frozen importlib._bootstrap>\", line 680, in _load_unlocked\n",
" File \"<frozen importlib._bootstrap_external>\", line 850, in exec_module\n",
" File \"<frozen importlib._bootstrap>\", line 228, in _call_with_frames_removed\n",
" File \"/var/task/lambda_function.py\", line 339, in <module>\n lambda_handler(1,1)\n",
" File \"/var/task/lambda_function.py\", line 263, in lambda_handler\n last_reported = int(read_db['Item']['last_reported']['N'])\n"
]
}
dynamo_table_name = 'my_table'
key = 'arn'
dynamo = boto3.client('dynamodb', region_name=source_region, config=Config(connect_timeout=5, read_timeout=60, retries={'max_attempts': 10}))
response = dynamo.query(
TableName=dynamo_table_name,
KeyConditionExpression="arn = :arn",
ExpressionAttributeValues={":arn": {"S":arn}}
)
if response == True
read_db = dynamo.get_item(TableName=dynamo_table_name, Key={'arn':{'S':arn}})
last_reported = int(read_db['Item']['last_reported']['N'])
print (last_reported)
答案1
得分: 1
你需要确保 get_item
返回了一个项目,可以通过检查返回的字典中是否存在 Item
键来实现:
read_db = dynamo.get_item(TableName=dynamo_table_name, Key={'arn':{'S':arn}})
if 'Item' in read_db:
# 你的逻辑
英文:
You need to be sure your get_item
returned an item, you can do that by checking for the existence of the Item
key in the dict returned:
read_db = dynamo.get_item(TableName=dynamo_table_name, Key={'arn':{'S':arn}})
if 'Item' in read_db:
# your logic
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论