gpt4 book ai didi

python - 无法使用python将项目添加到dynamodb表

转载 作者:行者123 更新时间:2023-11-28 21:56:36 24 4
gpt4 key购买 nike

我试图使用 boto 库向 dynamodb 表添加一个项目。这看起来很简单。我的代码是:

import boto.dynamodb

c = boto.dynamodb.connect_to_region(aws_access_key_id="xxx",
aws_secret_access_key="xxx",
region_name="us-west-2")

users = c.get_table("users")

users.put_item(data={'username': 'johndoe',
'first_name': 'John',
'last_name': 'Doe'})

但是,我收到以下错误:

'Table' object has no attribute 'put_item'

我想我连接到数据库很好,并且得到了 users 表(users 变量的类型是:boto.dynamodb.table。表)。所以,我不确定为什么它找不到 put_item 方法(我什至检查了 boto 库中 Table 类的代码,它有 put_item 方法).任何见解将不胜感激。

最佳答案

您正在使用 DynamoDB v1 界面。为此,请使用以下语法:

item_data = {
'Body': 'http://url_to_lolcat.gif',
'SentBy': 'User A',
'ReceivedTime': '12/9/2011 11:36:03 PM',
}

item = table.new_item(
# Our hash key is 'forum'
hash_key='LOLCat Forum',
# Our range key is 'subject'
range_key='Check this out!',
# This has the
attrs=item_data
)

来源:An Introduction to boto’s DynamoDB interface

我建议迁移到 DynamoDB v2 接口(interface):An Introduction to boto’s DynamoDB v2 interface

from boto.dynamodb2.table import Table

table = Table('users')

users.put_item(data={'username': 'johndoe',
'first_name': 'John',
'last_name': 'Doe'})

关于python - 无法使用python将项目添加到dynamodb表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21070938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com