flask服务器表示api调用被禁止,并抛出500个错误

r1zk6ea1  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(197)

我第一次做 flask 和python。下面的脚本由我的flask服务器执行,以执行一些数据转换。现在在底部,它调用了一个api writegetobjectresponse(),我在java服务器中编写了这个api,它正试图连接到该服务器。

import json
import boto3
import requests
from flask import Flask, request

lambda_server_url = 'http://127.0.0.1:8083/'
session = boto3.session.Session()
s3_client = session.client('s3', endpoint_url=lambda_server_url)
print('S3 client created!\n')
s3 = boto3.client('s3')

def lambda_handler(event, context):
    print('request headers - ', request.headers)
    object_get_context = event["getObjectContext"]
    request_route = object_get_context["outputRoute"]
    request_token = object_get_context["outputToken"]
    # s3_url = object_get_context["inputS3Url"]

    # Get object from S3
    response = request.get_data()
    print(response)
    original_object = response.decode("utf-8")

    # Transform object
    transformed_object = ''
    for ch in original_object:
        transformed_object = transformed_object + ch + ' '
    response = {
        'statusCode': 200,
        'body': json.dumps({'result': transformed_object})
    }

    print('/n/n*******Lambda Function Response Created*******/n/n', response)
    # Write object back to S3 Object Lambda

    s3.write_get_object_response(
        Body=transformed_object,
        RequestRoute=request_route,
        RequestToken=request_token)

    return {'status_code': 200}
    # return response

如果我把这个评论删掉 s3.write_get_object_response 在上面的脚本中,我得到了成功的响应。但一旦我启用此调用,它就会抛出一个错误,如 An error occurred (Forbidden) when calling the WriteGetObjectResponse operation: Forbidden. 应用程序代码:

app = Flask(__name__)
api = Api(app)

class lambdaFunction(Resource):
    def post(self, func_name):
        start_time = time.time()
        event = eventGenerator()
        return {'data': callFunc.dynamicCall(func_name,event),
                'execution time':time.time() - start_time}

api.add_resource(lambdaFunction, '/lambda/<func_name>')

if __name__ == '__main__':
     app.run()

callfunc是我的python文件,其中包含名为 dynamicCall() 它接受我的lambda函数名并从lambda_处理程序执行它。
详细调用堆栈-

{'getObjectContext': {'inputS3Url': <Request 'http://127.0.0.1:5000/lambda/space' [POST]>, 'outputRoute': 't1', 'outputToken': '...'}, 'userRequest': {'url': '...', 'headers': '...'}, 's3': {'s3SchemaVersion': '1.0', 'configurationId': '828aa6fc-f7b5-4305-8584-487c791949c1', 'bucket': {'name': 'test'}, 'object': {'name': 'name'}}}
request headers -  X-Request-Id: DD023720-3B38-4CC3-9C98-2DEA4DE38D13
Content-Type: application/x-www-form-urlencoded
User-Agent: Java/11.0.10
Host: 127.0.0.1:5000
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 36

[2021-07-08 20:55:44,547] ERROR in app: Exception on /lambda/space [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/usr/local/lib/python3.9/site-packages/flask_restful/__init__.py", line 467, in wrapper
    resp = resource(*args,**kwargs)
  File "/usr/local/lib/python3.9/site-packages/flask/views.py", line 83, in view
    return self.dispatch_request(*args,**kwargs)
  File "/usr/local/lib/python3.9/site-packages/flask_restful/__init__.py", line 582, in dispatch_request
    resp = meth(*args,**kwargs)
  File "/Users/atekade/hap/s3lambda/transformMicroservice/app.py", line 19, in post
    return {'data': callFunc.dynamicCall(func_name,event),
  File "/Users/atekade/hap/s3lambda/transformMicroservice/callFunc.py", line 8, in dynamicCall
    return getattr(mod, fcn_to_call)(event, LambdaContext)
  File "/Users/atekade/hap/s3lambda/transformMicroservice/space.py", line 36, in lambda_handler
    s3.write_get_object_response(
  File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 386, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 705, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (Forbidden) when calling the WriteGetObjectResponse operation: Forbidden.
127.0.0.1 - - [08/Jul/2021 20:55:44] "POST /lambda/space HTTP/1.1" 500 -

我应该如何解决这个问题?这里可能出了什么问题?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题