【发布时间】:2019-09-26 15:16:33
【问题描述】:
我是 Python 新手。我在 Lambda 函数中有一些 Python 代码,用于更新 DynamoDB 表 (ebsDaysToExpire) 中的 a 值。这样可行。当我想要获取新的更新值时,我会卡住,以便稍后在脚本中将它作为 send_mail 函数的一部分传递。
我已尝试添加 response = table.get_item 语句,但无法正常工作。
if response['Count'] == 0: #volume not being tracked in table
try:
response = table.put_item(
Item={
'volID': vid,
'ebsDaysToExpire': 7,
'snapshotStatus': 'incomplete',
'snapshotDate': 'incomplete',
'lifecycleStatus': 'start_7',
'snapshotID': 'incomplete',
'snapshotDaysToExpire': '30'
},
ConditionExpression='attribute_not_exists(volID)'
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
try:
response = table.update_item(
Key={
'volID': vid
},
UpdateExpression='set ebsDaysToExpire = ebsDaysToExpire + :val',
ExpressionAttributeValues={
':val': decimal.Decimal(-1)
},
ReturnValues='UPDATED_NEW'
)
except ClientError as e:
print(e.response['Error']['Message'])
【问题讨论】:
-
文档说:“您还可以使用
ReturnValues参数在同一 UpdateItem 操作中返回项目的属性值。”您的代码已经在执行此操作。因此,您无需再次致电get_item()。只需从update_item()中查看response。 -
ohlr,谢谢,您为我指明了正确的方向。感谢您的宝贵时间,我花了一段时间来梳理我想要从结果中得到什么,因为所有这些对我来说都很新鲜。我将在下面发布新代码。
标签: python python-3.x aws-lambda amazon-dynamodb boto3