我正在 AWS 中使用 python3、urllib3 和 PoolManager.request() 构建 Lambda
我的代码:
def process_message(token, message):
try:
bucket_name = message['s3']['bucket']['name']
object_name = message['s3']['object']['key']
print(f"|---> Processing message {bucket_name}: {object_name}")
# build request json
req_json = { 'Bucket': bucket_name, 'Object': object_name }
http = urllib3.PoolManager()
bearer_token = "Bearer " + token
auth_hdr = {'Authorization': bearer_token }
ct_hdr = {'Content-Type': 'application/json'}
obj_request_headers = {**auth_hdr, **ct_hdr}
new_object_response = http.request('POST', oracle_notifier_listener_url, json = req_json, headers = obj_request_headers)
logger.debug("Got new_object response: %s", new_object_response)
except Exception as err:
print("*---> An error occurred processing new object notification")
raise err
我收到错误:
{
"errorMessage": "HTTPResponse.__init__() got an unexpected keyword argument 'json'",
"errorType": "TypeError",
"requestId": "8d6c5511-6ca6-4245-8042-3aa5b3b98f89",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 23, in lambda_handler\n process_message(token, message)\n",
" File \"/var/task/lambda_function.py\", line 107, in process_message\n raise err\n",
" File \"/var/task/lambda_function.py\", line 102, in process_message\n new_object_response = http.request('POST', oracle_notifier_listener_url, json = req_json, headers = obj_request_headers)\n",
奇怪的是,请求似乎处理得很好,我可以在日志中看到 202 响应状态:
[DEBUG] 2024-10-15T17:05:11.150Z 8d6c5511-6ca6-4245-8042-3aa5b3b98f89 Starting new HTTPS connection (1): endpt.com:443
[DEBUG] 2024-10-15T17:05:12.375Z 8d6c5511-6ca6-4245-8042-3aa5b3b98f89 https://endpt:443 "POST /path/to/getObject HTTP/1.1" 202 None
我也尝试过:
new_object_response = http.request('POST', oracle_notifier_listener_url, json = json.dumps(req_json), headers = obj_request_headers)
我得到了相同的响应,相同的错误。
FWIW,我正在使用这些文档:https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html
全世界伟大的 Python 开发者,我做错了什么?
此错误表明您的 urllib3 版本太旧 (1.x)。
json
关键字参数是 urllib3
v2 中的新参数。升级urllib3.