我在我的程序中使用了coinmarketcap,并且我已将该库正确安装到我已上传到我的aws lambda函数的部署包中。但是,即使代码在我的机器上本地工作,它在aws lambda上运行时返回以下错误消息:
{
"stackTrace": [
[
"/var/task/main.py",
11,
"lambda_handler",
"return on_intent(event[\"request\"], event[\"session\"])"
],
[
"/var/task/main.py",
50,
"on_intent",
"data = coinmarketcap.ticker('Bitcoin', limit=3, convert='USD')"
],
[
"/var/task/coinmarketcap/core.py",
54,
"ticker",
"response = self.__request('ticker/' + currency, params)"
],
[
"/var/task/coinmarketcap/core.py",
28,
"__request",
"response_object = self.session.get(self.base_url + endpoint, params = params, timeout = self.request_timeout)"
],
[
"/var/task/coinmarketcap/core.py",
21,
"session",
"self._session = requests_cache.core.CachedSession(cache_name='coinmarketcap_cache', backend='sqlite', expire_after=120)"
],
[
"/var/task/requests_cache/core.py",
68,
"__init__",
"self.cache = backends.create_backend(backend, cache_name, backend_options)"
],
[
"/var/task/requests_cache/backends/__init__.py",
50,
"create_backend",
"return registry[backend_name](cache_name, **options)"
],
[
"/var/task/requests_cache/backends/sqlite.py",
28,
"__init__",
"self.responses = DbPickleDict(location + extension, 'responses', fast_save=fast_save)"
],
[
"/var/task/requests_cache/backends/storage/dbdict.py",
58,
"__init__",
"with self.connection() as con:"
],
[
"/usr/lib64/python2.7/contextlib.py",
17,
"__enter__",
"return self.gen.next()"
],
[
"/var/task/requests_cache/backends/storage/dbdict.py",
70,
"connection",
"con = sqlite.connect(self.filename)"
]
],
"errorType": "OperationalError",
"errorMessage": "unable to open database file"
}
代码:
Was Here
关于为什么会发生这种情况的任何想法?
答案 0 :(得分:1)
当尝试在coinmarketcap_cache.sqlite
中创建一个名为/var/task/
的本地文件时,coinmarketcap-api代码(看看here)失败了(你会在本地机器上找到它,这是什么原因)它适用于本地)。
AWS Lambda没有/var/task/
的写入权限。但是,它具有/tmp/
的写入权限。遗憾的是,coinmarketcap-api不支持更改该文件的位置,这使您只有很少的选择:
尝试在代码的顶部更改当前目录。
import os
os.chdir("/tmp")