Rest API with file transfer

Hi,
I’d like to set up an Rest API doing the following steps:

  1. Read input parameters for workflow in JSON format (“Container input (JSON)” node)
  2. Read in file from path defined in input JSON
  3. Manipulate and analyze this image
  4. Store modified image as one part of the result
  5. Return results including path to result image as JSON (“Container output (JSON)” node)

My question is: Where should I store the modified image?

I found out that “knime://knime.workflow” will store it in the temporary workflow directory. But how long does the image live there? Will it be deleted right after the execution of the workflow? The external user of the Rest Api will receive the path to the result image and wants to load it.

Thanks for all hints in advance and best regards

1 Like

You could save it in a temporary directory or within the workflow’s data folder, e.g. knime://knime.workflow/data. This way the image would be available until the workflow will be discarded. Another option would be to save the image into the server repository (could be accessed via REST) or some 3rd party data store (might need another workflow to access the image).

Thanks a lot! So will try to store it in the temporary data folder. When is the workflow discarded? Can I control it when calling the Rest Api?

By default a job is discarded after 7 days, but you can adjust the parameter com.knime.server.job.max_lifetime= in knime-server.config.

You can also discard jobs via a REST call, in case you want to delete it once it has been downloaded.

Thank you! I successfully stored the file in the temporary. But I I fear that in our current setting we don’t have REST API access to files in there. What solution would you propose? Is a “Download” node usable within this setting the right thing?

Indeed, adding a File Download Widget node and using the temp-file-path in its configuration should do the trick for you.
image

Even though Widget nodes are mainly designed to be used as input in interactive views, they will also provide an output resource that can be accessed via GET REST to the job endpoint:

http://{knime-server-address}/knime/rest/v4/jobs/{job-ID}/output-resources/{resourceId}

Thanks for clearly pointing this out. So I am almost able to set this up. The hopefully last hurdle is the missing possibility to expose the path from the “Image writer” node:
image
Is there a place where I have to enable this option for this node type?

I guess you have configured the node to write an image to a previously created temp directory via flow variable. You can use the same flow variable in File Download node, just connect them as shown in the example above.

Yes I have it configured like that way. I use the “Image writer” and here I have to define the path to the file by a column entry. Therefore I first convert the variable to a column:

image

But I got still the message

“message”: “Variable “filePath” does not denote an existing file: knime://knime.workflow/tmp/resFilesb11waqzt0gid/rotImage.png”

when calling the rest API. Any further hints are really appreciated.

Maybe you have a workflow like the following at hand and could share it.

Rest API receives json data including path to an image file + stores this image in a temporary folder + provides a file download option

Then I could integrate my image manipulation and a second output with further results in json format.

Did you reset the workflow before upload (or execution)? From the screenshot it looks like Create Temp Dir and Create File Name node were saved in executed state, therefore won’t be executed on the job and won’t create any tmp directories.

Sorry, the screenshot is in that case misleading. But yes I resetted the workflow before upload.

I got it. The “Image writer” has the specialty that it adds the extension to the path. So I needed two flow variables, one without the extension for “Image writer” and one with the extension for the “File Download” node.
Thanks for your support!!!

2 Likes