【问题标题】:Why this dynamodb query failed with 'Requested resource not found'?为什么这个 dynamodb 查询因“未找到请求的资源”而失败?
【发布时间】:2020-04-25 00:29:25
【问题描述】:

我再次检查了该项目是否存在于 dynamodb 表中。 id 是默认的哈希键。

我想在这段代码中使用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 版

【问题讨论】:

  • 您在 AWS 控制台中查看的是哪个区域?
  • us-west-2。查看代码中的变量region

标签: python-2.7 amazon-dynamodb boto


【解决方案1】:

问题出在这段代码中:

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

应该是

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

注意connection 命名参数

【讨论】:

    【解决方案2】:

    如果您有一个范围键,则必须在 get_item 中指定,如下所示:

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

    在我的表包中,我有一个索引(id)和一个范围键(时间戳)。

    【讨论】:

      【解决方案3】:

      我收到此错误是因为我连接到了错误的区域。

      要检查您的表区域,请转到表的概览选项卡并向下滚动到 Amazon 资源名称 (ARN) 字段。

      我的 ARN 以 arn:aws:dynamodb:us-east-2: 开头。这里的“us-east-2”是我在启动 boto3 客户端时需要经过的区域。

      【讨论】:

        猜你喜欢
        • 2015-10-11
        • 1970-01-01
        • 1970-01-01
        • 2015-04-21
        • 1970-01-01
        • 1970-01-01
        • 2016-11-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多