关于 python:DynamoDB 插入(放置)项目 – 返回创建的项目?

DynamoDB insert (put) item - return the item created?

在我的 Python 脚本中,我将一条记录(项目)插入到 DynamoDB 表中,但我意识到来自 AWS 的响应不包括实际插入的项目:

1
2
3
4
5
6
7
8
9
10
{'ResponseMetadata': {'HTTPHeaders': {'connection': 'keep-alive',
   'content-length': '2',
   'content-type': 'application/x-amz-json-1.0',
   'date': 'Fri, 27 Jan 2017 23:31:22 GMT',
   'server': 'Server',
   'x-amz-crc32': '234324243',
   'x-amzn-requestid': 'xxxxxxxxxx'},
  'HTTPStatusCode': 200,
  'RequestId': 'xxxxxxxxxx',
  'RetryAttempts': 0}}

这很不幸,因为我有这样的逻辑:

1
2
3
4
5
6
if not user.exists():
    user = create_user()
else:
    user = get_user()

user.do_something()

而且我宁愿不要为了获取我刚刚插入的用户对象而再进行一次往返。

当然,我可以模拟对象,因为在 Python 中使用 boto3user 对象只是一个类似于字典的东西,但是能够只使用一个表示形式就很好了。似乎 put_item 调用

1
r = user_table.put_item(Item={'user_id': uid, 'info' : { ... }})

应该能够退回放置的物品吗?或者其他一些端点可以?

感谢您的任何见解!


您需要在您的 put_item 请求中设置属性 ReturnValues='ALL_NEW' 以在响应中取回该项目。如此处所述

编辑:文档有误,put_item 不接受 ALL_NEW。由于您在内存中有确切的项目作为您传递给 put_item 的参数,因此您真的不需要返回它。