2015-04-01 52 views
2

我已經加倍檢查該項目是否存在於dynamodb表中。 id是默認的散列鍵。爲什麼這個dynamodb查詢失敗,'請求資源未找到'?

enter image description here

我想在此代碼中使用main函數來獲取內容:

import boto.dynamodb2 
from boto.dynamodb2 import table 

table='doc' 
region='us-west-2' 
aws_access_key_id='YYY' 
aws_secret_access_key='XXX' 

def get_db_conn(): 
    return boto.dynamodb2.connect_to_region(
      region, 
      aws_access_key_id=aws_access_key_id, 
      aws_secret_access_key=aws_secret_access_key) 

def get_table(): 
    return table.Table(table, get_db_conn()) 

def main(): 
    tbl = get_table() 
    doc = tbl.get_item(id='4d7a73b6-2121-46c8-8fc2-54cd4ceb2a30') 
    print doc.keys() 

不過,我得到這個異常,而不是:

File "scripts/support/find_doc.py", line 31, in <module> 
    main() 
    File "scripts/support/find_doc.py", line 33, in main 
    doc = tbl.get_item(id='4d7a73b6-2121-46c8-8fc2-54cd4ceb2a30') 
    File "/Users/antkong/project-ve/lib/python2.7/site-packages/boto/dynamodb2/table.py", line 504, in get_item 
    consistent_read=consistent 
    File "/Users/antkong/project-ve/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 1065, in get_item 
    body=json.dumps(params)) 
    File "/Users/antkong/project-ve/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 2731, in make_request 
    retry_handler=self._retry_handler) 
    File "/Users/antkong/project-ve/lib/python2.7/site-packages/boto/connection.py", line 953, in _mexe 
    status = retry_handler(response, i, next_sleep) 
    File "/Users/antkong/project-ve/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 2774, in _retry_handler 
    data) 
boto.exception.JSONResponseError: JSONResponseError: 400 Bad Request 
{u'message': u'Requested resource not found', u'__type': u'com.amazonaws.dynamodb.v20120810#ResourceNotFoundException'} 

爲什麼我得到這個錯誤信息?

我使用boto版本2.34

+0

您在AWS控制檯中查看哪些區域? – mkobit 2015-04-01 18:01:34

+0

us-west-2。請參閱代碼中的變量'region' – 2015-04-02 00:38:58

回答

0

的問題是在此代碼:

def get_table(): 
    return table.Table(table, get_db_conn()) 

應該

def get_table(): 
    return table.Table(table, connection=get_db_conn()) 

注意connection命名參數

1

如果你有你必須在get_item中指定一個範圍鍵,像這樣:

get_item(timestamp=Decimal('1444232509'), id='HASH_SHA1') 

這裏在我的表包我有一個索引(id)和範圍鍵(時間戳)。

相關問題