【问题标题】:Update DynamoDB Table then get new value更新 DynamoDB 表然后获取新值
【发布时间】: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


【解决方案1】:

这就是我的代码现在的样子,它会在“table_put.item”更新表后从 DynamoDB 表中返回新值(返回值)。这将作为“xdays”传递。感谢 ohlr 的帮助。

            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'
                    )
                    xdays = response['Attributes']['ebsDaysToExpire']
                    print xdays
                except ClientError as e:
                    print(e.response['Error']['Message'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    相关资源
    最近更新 更多