如何使用 python 将项目放入 DynamoDB table?

How to put Items into DynamoDB table using python?

我正在尝试使用 python 脚本 put 将项目 table 放入 Amazon DynamoDB table,但是当我 运行 python 脚本我收到以下错误:

Traceback (most recent call last):
  File "./table.py", line 32, in <module>
    item.put(None, None)
  File "/usr/local/lib/python2.7/dist-packages/boto/dynamodb/item.py", line 183, in put
    return self.table.layer2.put_item(self, expected_value, return_values)
  File "/usr/local/lib/python2.7/dist-packages/boto/dynamodb/layer2.py", line 551, in put_item
    object_hook=self.dynamizer.decode)
  File "/usr/local/lib/python2.7/dist-packages/boto/dynamodb/layer1.py", line 384, in put_item
    object_hook=object_hook)
  File "/usr/local/lib/python2.7/dist-packages/boto/dynamodb/layer1.py", line 119, in make_request
    retry_handler=self._retry_handler)
  File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 954, in _mexe
    status = retry_handler(response, i, next_sleep)
  File "/usr/local/lib/python2.7/dist-packages/boto/dynamodb/layer1.py", line 159, in _retry_handler
    data)
boto.exception.DynamoDBResponseError: DynamoDBResponseError: 400 Bad Request
{u'message': u'Requested resource not found', u'__type': u'com.amazonaws.dynamodb.v20111205#ResourceNotFoundException'}

我的代码是:

#!/usr/bin/python

import boto
import boto.s3
import sys

from boto import dynamodb2
from boto.dynamodb2.table import Table
from boto.s3.key import Key
import boto.dynamodb

conn = boto.dynamodb.connect_to_region('us-west-2', aws_access_key_id=<My_access_key>, aws_secret_access_key=<my_secret_key>)

entity = conn.create_schema(hash_key_name='RPI_ID', hash_key_proto_value=str, range_key_name='PIC_ID', range_key_proto_value=str)
table = conn.create_table(name='tblSensor', schema=entity, read_units=10, write_units=10)


item_data = {
        'Pic_id': 'P100',
        'RId': 'R100',
        'Temperature': '28.50'
    }
item = table.new_item(
        # Our hash key is 'forum'
        hash_key='RPI_ID',
        # Our range key is 'subject'
        range_key='PIC_ID',
        # This has the
        attrs=item_data
    )

item.put() // I got error here.

我的参考是:Setting/Getting/Deleting CORS Configuration on a Bucket

我 运行 您的代码在我的帐户中,它 100% 完美运行,返回:

{u'ConsumedCapacityUnits': 1.0}

您可能需要检查您使用的是否是最新版本的 boto:

pip install boto --upgrade

我在 google 上搜索并解决了我的问题。我已经在我的 raspberry pi 板上设置了正确的 timedate 并且 运行 那个程序工作正常。