AWS Lambda 上的 Python:来自 botocore.vendored 的“请求”已弃用,但“请求”不可用

2024-01-24

我有一个用于 AWS Lambda 函数的 Python 脚本,该函数向另一个端点发出 HTTP POST 请求。自从Python的urllib2.request, https://docs.python.org/2/library/urllib2.html https://docs.python.org/2/library/urllib2.html,只能处理标准中的数据application/x-www-form-urlencoded格式并且我想发布 JSON 数据,我使用了 Requests 库,https://pypi.org/project/requests/2.7.0/ https://pypi.org/project/requests/2.7.0/.

该 Requests 库在 Python 运行时环境中的 AWS Lambda 上不可用,因此必须通过以下方式导入from botocore.vendored import requests。到目前为止,一切都很好。

今天,我收到了关于此的弃用警告:

DeprecationWarning: You are using the post() function from 'botocore.vendored.requests'.
This is not a public API in botocore and will be removed in the future.
Additionally, this version of requests is out of date. We recommend you install the
requests package, 'import requests' directly, and use the requests.post() function instead.

AWS 的这篇博文中也提到了这一点:https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/ https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/.

不幸的是,改变from botocore.vendored import requests into import requests结果出现以下错误:

No module named 'requests'

Why is requests不适用于 AWS Lambda 上的 Python 运行时?我该如何使用/导入它?


我成功使用以下命令发送 HTTP POST 请求urllib3库,可从 AWS Lambda 获取,无需额外的安装说明。

import urllib3

http = urllib3.PoolManager()

response = http.request('POST',
                        url,
                        body = json.dumps(some_data_structure),
                        headers = {'Content-Type': 'application/json'},
                        retries = False)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AWS Lambda 上的 Python:来自 botocore.vendored 的“请求”已弃用,但“请求”不可用 的相关文章

随机推荐