python-3.x 如何修复AttributeError:模块“botocore.vendored.requests”没有属性“Post”跟踪

b0zn9rqh  于 2022-12-14  发布在  Python
关注(0)|答案(3)|浏览(147)

我试图让我的LexBot与Lambda通信,所以我遵循了创建请求、签名和所需的一切的过程。但我不确定是否需要从Python导入某些东西。当我准备发布消息时,它失败了。签名和auth头文件都创建好了。
请各位指教,任何帮助将不胜感激!
我以这些文章为指导:https://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html
https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

print ('\nBEGIN REQUEST++++++++++++++++++++++++++++++++++++')
print ('Request URL = ' + endpoint)
print('\n' + authorization_header)
print('\nX-Amz-content-Sha256 header is' + payload_hash)

r = requests.Post ('myendpoint'+ canonical_uri, data=payload, 
headers=headers)
data = r.json()
lex_message = data['message']
print ('' + lex_message)

所以我假设我的问题来自这段代码,注意,我删除了我的端点。
也不确定请求中的数据包含什么内容。

aij0ehis

aij0ehis1#

我遇到了同样的错误,但对于GET使用python3.8。对GET/POST的支持已经从python3.8中的botocore.vendored.requests中删除。
作为一种解决方法,您可以使用python3.7作为lambda,它在一段时间内仍然有效。但是,您将得到一个警告,对它的支持也将在2020/03/31上删除:

DeprecationWarning: You are using the put() function from 'botocore.vendored.requests'.  This dependency was removed from Botocore and will be removed from Lambda after 2020/03/31. https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/. Install the requests package, 'import requests' directly, and use the requests.put() function instead.

因此,使用pip获得requests包,还是使用下面链接中显示的层,没有太多选择。
更多信息请参见Upcoming changes to the Python SDK in AWS Lambda

hiz5n14c

hiz5n14c2#

在这个拉取请求中,他们建议不要再使用requests https://github.com/aws-quickstart/taskcat/pull/343的供应商版本。不幸的是,许多AWS代码示例仍然使用它。
您可以使用官方的“requests”库(https://requests.readthedocs.io/en/master/),替换为:

from botocore.vendored import requests

import requests
ajsxfq5m

ajsxfq5m3#

下面是来自AWS的一个示例,它显示了如何使用urllib3库来实现相同的https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-lambda-function-code-cfnresponsemodule.html
在此放置相关部分,以防链接失效:

import urllib3
...

http = urllib3.PoolManager()

responseBody = { 'foo': 'bar' }

json_responseBody = json.dumps(responseBody)

headers = { ... }

try:
    response = http.request('PUT', responseUrl, headers=headers, body=json_responseBody)

    print("Status code:", response.status)

except Exception as e:

    handle error

另外,我已经测试过了,这段代码可以在Python 3.9 Lambda Runtime中开箱即用,不需要安装额外的软件包

相关问题