0

I'm writing an OpenWhisk action in Python 3, which needs to read a local file. It seems once I create an OpenWhisk action with this python file and invoke it, the python action can't read this local file anymore. The local file locates in the same dir as the python file does. The logs reads stderr: IOError: [Errno 2] No such file or directory: './localFile'

Does any one know how to access a file from within an OpenWhisk action?

Jane Zhao
  • 51
  • 6
  • Could you provide an example on the steps you are taking to create the action. – csantanapr Mar 31 '17 at 23:59
  • If you are using the OpenWhisk being offer by IBM on Bluemix, it doesn't support python 3 yet as a default runtime. The other problem you would would have you need to create a zip file that contains both your python script and the file your are trying to read. This is capability that it's supported on the open source version of OpenWhisk on github https://github.com/openwhisk/openwhisk available soon on bluemix.net – csantanapr Apr 01 '17 at 00:02
  • If you want to support this today on the offering from bluemix.net, you would need to go with a docker action, this allows you to have any language, linux distro, and include any files you want. – csantanapr Apr 01 '17 at 00:03
  • if it's just a zip file (containing python modules and extra files), this is supported already in bluemix and in the open source master. – user6062970 Apr 01 '17 at 14:07
  • 1
    I suspect the issue here is that the script is running from a different directory than the zip file. This needs the equivalent of `__dirname` for nodejs action (should be `/action` in this case). – user6062970 Apr 01 '17 at 14:14
  • I think user6062970 is right. The action is probably executed in a different dir and I need to find where the dir is. – Jane Zhao Apr 04 '17 at 19:37

1 Answers1

1

The problem is that a relative reference to a file will not work, since the source code runs from a different direction (/action) compared to the internal runtime. This can be remedied in a brute force way by not referencing files using a relative path (e.g., ./).

For example, this will work - but no ideal:

$ cat __main__.py
def main(args):
  f = open('/action/workfile', 'r')
  return {'file': f.read()}


$ echo "hello" > workfile

$ zip p.zip __main__.py workfile

$ wsk action update p p.zip --kind python:3
ok: updated action p

$ wsk action invoke -br p
{
    "file": "hello\n"
}
user6062970
  • 831
  • 4
  • 5
  • This issue offers a patch against the runtime to allow for relative paths to work as expected https://github.com/openwhisk/openwhisk/issues/2095. – user6062970 Apr 01 '17 at 14:35
  • It seems I don't have an /action folder. Will look into your suggestions. Thank you. – Jane Zhao Apr 04 '17 at 19:40