我为AWS函数提供了一个Python脚本,该函数可以向另一个端点发送HTTP POST请求。由于Python的urllib2.request https://docs.python.org/2/library/urllib2.html只能处理标准application/x-www-form-urlencoded格式的数据,并且我想发布JSON数据,所以我使用了请求库https://pypi.org/project/requests/2.7.0/。
在Python运行时环境中,AWS无法使用该请求库,因此必须通过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/。
不幸的是,将from botocore.vendored import requests更改为import requests会导致以下错误:
No module named 'requests'为什么requests不能用于AWS的Python运行时?我如何使用/导入它?
发布于 2019-10-18 07:44:08
如果您使用的是无服务器框架
在serverless.yml中指定插件
plugins:
- serverless-python-requirements在根目录下创建文件requirements.txt
requirements.txt
requests==2.22.0这将安装上述请求和包。
发布于 2019-10-18 10:34:44
我成功地使用urllib3库发送HTTP请求,该库在available中可用,无需附加安装说明。
import urllib3
http = urllib3.PoolManager()
response = http.request('POST',
url,
body = json.dumps(some_data_structure),
headers = {'Content-Type': 'application/json'},
retries = False)发布于 2020-03-31 02:10:52
查看这里的说明:https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-dependencies
您所需要做的就是在本地下载请求模块,然后将其包含在Lambda函数部署包(ZIP归档)中。
示例(如果所有Lambda函数都由一个Python模块+请求模块组成):
$ pip install --target ./package requests
$ cd package
$ zip -r9 ${OLDPWD}/function.zip .
$ cd $OLDPWD
$ zip -g function.zip lambda_function.py
$ aws lambda update-function-code --function-name my-function --zip-file fileb://function.ziphttps://stackoverflow.com/questions/58445984
复制相似问题