简体   繁体   English

IBM云功能 - 如何使用和上传自己的库?

[英]IBM Cloud Functions - How to use and upload own libraries?

Is it possible to use / upload own libraries to IBM Cloud Functions? 是否可以使用/上传自己的库到IBM Cloud Functions? Or is it limited to the preinstalled packages? 或者仅限于预装的包装? I plan to use Python as programming language. 我打算用Python作为编程语言。

You can bundle your own dependencies. 您可以捆绑自己的依赖项。 See the docs here https://github.com/apache/incubator-openwhisk/blob/master/docs/actions-python.md#packaging-python-actions-with-a-virtual-environment-in-zip-files for creating a virtual environment with your libraries. 请参阅此处的文档https://github.com/apache/incubator-openwhisk/blob/master/docs/actions-python.md#packaging-python-actions-with-a-virtual-environment-in-zip-files使用库创建虚拟环境。 The docs provide an example installing dependencies via requirements.txt . 文档提供了通过requirements.txt安装依赖项的示例。

It is possible to use more libraries than the preinstalled ones. 可以使用比预先安装的库更多的库。 There are some tips & tricks in the IBM Cloud Functions docs and the linked blog articles, eg, here for Python . IBM Cloud Functions文档和链接的博客文章中有一些提示和技巧,例如,这里是Python

For Python you can either use a virtual environment and package that up or use a zip file with the required Python files. 对于Python,您可以使用虚拟环境并将其打包或使用带有所需Python文件的zip文件。 The virtual environment might be easier to start with, but you could end up with a lot of unnecessary files. 虚拟环境可能更容易入手,但最终可能会有大量不必要的文件。 What I prefer is to download the required files and put them into a zip file on my own. 我更喜欢的是下载所需的文件并将它们放入我自己的zip文件中。 Of course, this is only manageable to a certain degree. 当然,这只能在一定程度上进行管理。

I used that method in this IBM Cloud solution tutorial on serverless GitHub traffic statistics . 我在这个关于无服务器GitHub流量统计的IBM Cloud解决方案教程中使用了这种方法。 You can find the source code, including the zip file I created for the Python action, in this GitHub repository (see the functions folder ). 您可以在此GitHub存储库中找到源代码,包括我为Python操作创建的zip文件 (请参阅函数文件夹 )。

You can use any docker image to execute your actions, as long as the images are available on Docker Hub. 只要图像在Docker Hub上可用,您就可以使用任何泊坞窗图像来执行您的操作。 So you can create your own image with your libraries. 因此,您可以使用库创建自己的图像。

So, for example, if you want your own image that adds the python library yattag , a library generating HTML from python code. 所以,例如,如果你想要你自己的图像添加python库yattag ,一个从python代码生成HTML的库。

you can write a Dockerfile like this: 你可以像这样写一个Dockerfile:

FROM openwhisk/python3action
RUN pip install yattag

and then build and push 然后构建并推送

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

Now you have a public image you can use in OpenWhisk/IBM Cloud. 现在,您可以在OpenWhisk / IBM Cloud中使用公共映像。

Here is a simple python hello world using yattag: 这是一个使用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

create and run the action: 创建并运行操作:

$ 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM