gpt4 book ai didi

python - Flask 下 Botox 的 Dynamodb JSON 响应错误

转载 作者:行者123 更新时间:2023-12-02 08:53:28 26 4
gpt4 key购买 nike

我有这个模块,它连接到 Dynamo,创建一个 session 表,并在其中放入一个项目:

import boto.dynamodb2
from boto.dynamodb2.table import Table
from boto.dynamodb2.fields import HashKey
from boto.regioninfo import RegionInfo
from boto.dynamodb2.layer1 import DynamoDBConnection
import uuid

try:
sessions = Table(
table_name='IssueBoxSessions',
schema=[HashKey('SessionId')],
connection=DynamoDBConnection(
region=RegionInfo(name='eu-west-1',
endpoint='dynamodb.eu-west-1.amazonaws.com')
))
except:
print("Dynamo can't connect")

def create_session():
id = str(uuid.uuid4())
res = sessions.put_item(data={
'SessionId': id,
'data': {
'user_id': 1,
'ip': '10.1.1.10',
'datetime': 'now'
}
})
print(res)
return res

请注意,我正在 EC2 实例上进行开发和测试,并使用附加到该实例的 IAM 角色向 Dynamo 进行身份验证。

因为我使用的是 IAM,所以不需要任何代码进行身份验证。它们允许实体(我的实例)访问某些服务(发电机)。它们旨在防止必须将访问 key 放入某些 AWS 资源的代码中。如果是身份验证的情况,它在解释器中也不起作用。我确信这个问题是由 IAM 身份验证之外的其他原因造成的。

因此,当我在 python 解释器中导入此 create_session 函数并调用它时,它会起作用:

>>> from issuesite.session_handler import create_session
>>> create_session()
True
True

但是,当我尝试在 flask 附近的任何地方使用它时,它会熔化:

from flask import request
from issuesite.session_handler import create_session
from issuesite import app

@app.route('/login', methods=['GET'])
def login():
if create_session():
return "ok"

抛出的错误是:

JSONResponseError: JSONResponseError: 400 Bad Request
{u'message': u'The security token included in the request is invalid.', u'__type': u'com.amazon.coral.service#UnrecognizedClientException'}

事实上,这是整个回溯的美妙之处:

Traceback (most recent call last):
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/server/issuesite/views/base.py", line 14, in login
if create_session():
File "/server/issuesite/session_handler/__init__.py", line 29, in create_session
'datetime': 'now'
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/table.py", line 821, in put_item
return item.save(overwrite=overwrite)
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/items.py", line 455, in save
returned = self.table._put_item(final_data, expects=expects)
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/table.py", line 835, in _put_item
self.connection.put_item(self.table_name, item_data, **kwargs)
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 1510, in put_item
body=json.dumps(params))
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 2842, in make_request
retry_handler=self._retry_handler)
File "/server/flask/lib/python2.7/site-packages/boto/connection.py", line 954, in _mexe
status = retry_handler(response, i, next_sleep)
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 2885, in _retry_handler
data)

最佳答案

根据您发布的代码,您使用的是 Boto 的常规连接方法,而您应该使用 IAM 专用的连接方法 ( http://boto.readthedocs.org/en/latest/ref/iam.html )。

我希望这会有所帮助。

关于python - Flask 下 Botox 的 Dynamodb JSON 响应错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31915778/

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