如何正确使用boto3的if语句

huangapple go评论66阅读模式
英文:

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

  {
    &quot;errorMessage&quot;: &quot;&#39;Item&#39;&quot;,
    &quot;errorType&quot;: &quot;KeyError&quot;,
    &quot;requestId&quot;: &quot;&quot;,
    &quot;stackTrace&quot;: [
    &quot;  File \&quot;/var/lang/lib/python3.9/importlib/__init__.py\&quot;, line 127, in import_module\n    
    return _bootstrap._gcd_import(name[level:], package, level)\n&quot;,
   &quot;  File \&quot;&lt;frozen importlib._bootstrap&gt;\&quot;, line 1030, in _gcd_import\n&quot;,
&quot;  File \&quot;&lt;frozen importlib._bootstrap&gt;\&quot;, line 1007, in _find_and_load\n&quot;,
&quot;  File \&quot;&lt;frozen importlib._bootstrap&gt;\&quot;, line 986, in _find_and_load_unlocked\n&quot;,
&quot;  File \&quot;&lt;frozen importlib._bootstrap&gt;\&quot;, line 680, in _load_unlocked\n&quot;,
&quot;  File \&quot;&lt;frozen importlib._bootstrap_external&gt;\&quot;, line 850, in exec_module\n&quot;,
&quot;  File \&quot;&lt;frozen importlib._bootstrap&gt;\&quot;, line 228, in _call_with_frames_removed\n&quot;,
&quot;  File \&quot;/var/task/lambda_function.py\&quot;, line 339, in &lt;module&gt;\n    lambda_handler(1,1)\n&quot;,
&quot;  File \&quot;/var/task/lambda_function.py\&quot;, line 263, in lambda_handler\n    last_reported = int(read_db[&#39;Item&#39;][&#39;last_reported&#39;][&#39;N&#39;])\n&quot;

]
}

  dynamo_table_name = &#39;my_table&#39;
  key = &#39;arn&#39;
  dynamo = boto3.client(&#39;dynamodb&#39;, region_name=source_region,  config=Config(connect_timeout=5, read_timeout=60, retries={&#39;max_attempts&#39;: 10}))
          response = dynamo.query(
            TableName=dynamo_table_name, 
            KeyConditionExpression=&quot;arn = :arn&quot;,
            ExpressionAttributeValues={&quot;:arn&quot;: {&quot;S&quot;:arn}}
            )
          if response == True
            read_db = dynamo.get_item(TableName=dynamo_table_name, Key={&#39;arn&#39;:{&#39;S&#39;:arn}})
            last_reported = int(read_db[&#39;Item&#39;][&#39;last_reported&#39;][&#39;N&#39;])
            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={&#39;arn&#39;:{&#39;S&#39;:arn}})

if &#39;Item&#39; in read_db:
    # your logic

huangapple
  • 本文由 发表于 2023年2月9日 01:01:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定