IBM Cloud Functions-如何使用和上传自己的库?

时间:2018-08-17 19:16:29

标签: python ibm-cloud openwhisk ibm-cloud-functions

是否可以使用/将自己的库上传到IBM Cloud Functions?还是仅限于预装的软件包?我打算将Python用作编程语言。

3 个答案:

答案 0 :(得分:3)

您可以捆绑自己的依赖项。有关使用库创建虚拟环境的信息,请参见此处https://github.com/apache/incubator-openwhisk/blob/master/docs/actions-python.md#packaging-python-actions-with-a-virtual-environment-in-zip-files的文档。该文档提供了一个通过requirements.txt安装依赖项的示例。

答案 1 :(得分:1)

与预安装的库相比,可以使用更多的库。有一些tips & tricks in the IBM Cloud Functions docs and the linked blog articles, e.g., here for Python

对于Python,您可以使用虚拟环境并对其进行打包,也可以将zip文件与所需的Python文件一起使用。虚拟环境一开始可能会比较容易,但是最终可能会导致很多不必要的文件。我更喜欢下载所需的文件,然后自己将它们放入一个zip文件中。当然,这只能在一定程度上进行管理。

我在此IBM Cloud solution tutorial on serverless GitHub traffic statistics中使用了该方法。您可以找到source code, including the zip file I created for the Python action, in this GitHub repository(请参阅functions folder)。

答案 2 :(得分:-1)

您可以使用任何Docker映像执行操作,只要这些映像在Docker Hub上可用即可。因此,您可以使用库创建自己的图像。

例如,如果您想要自己的图像添加python库yattag,该库是根据python代码生成HTML的库。

您可以这样编写Dockerfile:

FROM openwhisk/python3action
RUN pip install yattag

然后构建并推送

$ docker build -t docker.io/msciab/python3action-yattag:latest .
$ docker push docker.io/msciab/python3action-yattag

现在您有了可以在OpenWhisk / IBM Cloud中使用的公共映像。

这是一个使用yattag的简单python hello世界:

from yattag import Doc

def main(dict):
  doc, tag, text = Doc().tagtext()
  with tag('h1'):
     text('Hello world!')
  dict['body'] = doc.getvalue()
  return dict

创建并运行操作:

$ wsk action create  hello-yattag hello.py --web true --docker msciab/python3action-yattag
$ curl $(wsk action get hello-yattag --url|tail -1)
<h1>Hello world!</h1>