更新DynamoDB表,然后获取新值

2024-09-28 20:55:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还不熟悉。我在一个Lambda函数中用Python编写了一段代码,该函数更新DynamoDB表(ebsDaysToExpire)中的a值。这很管用。当我想获取新的更新值,以便稍后在脚本中作为send\u 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'])

Tags: 函数responseastablenoterroritemprint
1条回答
网友
1楼 · 发布于 2024-09-28 20:55:14

这就是我的代码现在的样子,它将从DynamoDB表中的'table'后面返回新值_放置项目'更新表(返回值)。这将被传递为“xdays”。感谢人权高专办的协助。你知道吗

            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'])

相关问题 更多 >