我在程序中使用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


关于为什么会这样的想法?

最佳答案

尝试在coinmarketcap_cache.sqlite中创建名为/var/task/的本地文件时,coinmarketcap-api代码(请看here)失败(您将在本地计算机上找到它,这就是为什么在本地运行的原因)。

AWS Lambda没有对/var/task/的写权限。但是,它具有对/tmp/的写权限。遗憾的是,coinmarketcap-api不支持更改该文件的位置,这使您几乎没有选择:

尝试在代码的最上面更改当前目录。

import os
os.chdir("/tmp")

08-07 13:59