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

泄露秘密 提交于 2019-12-24 11:35:35

问题


Is it possible to use / upload own libraries to IBM Cloud Functions? Or is it limited to the preinstalled packages? I plan to use Python as programming language.


回答1:


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. The docs provide an example installing dependencies via requirements.txt.




回答2:


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, e.g., here for Python.

For Python you can either use a virtual environment and package that up or use a zip file with the required Python files. 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. 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. You can find the source code, including the zip file I created for the Python action, in this GitHub repository (see the functions folder).




回答3:


You can use any docker image to execute your actions, as long as the images are available on 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.

you can write a Dockerfile like this:

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.

Here is a simple python hello world using yattag:

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>


来源:https://stackoverflow.com/questions/51901638/ibm-cloud-functions-how-to-use-and-upload-own-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!