Attach a File with REST and Python

This example attaches a file to the Clarity LIMS server file storage repository instead of linking to an existing file on the network. For more information on linking to an existing file, refer to is covered in Attach Files Located Outside the Default File Storage Repository.

Both examples are useful in practice, depending on your network, storage architecture, and the size of the file. The file attachment method used in this example is equivalent to a lab scientist manually importing a file and attaching it to a file placeholder in the Clarity LIMS user interface.

Prerequisites

Before you follow the example, make sure you have the following items:

  • Files can be attached to projects, steps or result file artifacts.

  • This script uses the python package Requests and relies upon glsapiutil.py.

  • A compatible version of API (v2).

Use EPP/Automation to Attach Files

You can also use EPP/automation to handle attaching files. Though this method is not as flexible, it does attach a file automatically. With this method, the attached files are copied to the Clarity LIMS server and attached based on a LIMS ID in the created file name. Automation scripts are powerful, but they are only called when the process/step is created, whereas the code in this example can run at any time. For more information, see Attach a File to a File Placeholder with REST.

Code Example

Attaching a file requires the use of glsstorage and files resources.

The glsstorage resource assigns a file location, much like a file placeholder in the user interface. The files resource is used to associate the physical disk location to the ResultFile artifact. In combination, these resources allow flexible management of files, and the integration of external file manipulation and transfer tools.

Attaching a file is done in three main steps:

  1. Using a POST, create a storage location for the file to glsstorage. This returns the XML that POSTs to the files resource. The XML includes a content-location.

  2. Link the file to the placeholder (creating a unique file LIMS ID) by POSTing the glsstorage response to the /api/v2/files endpoint.

  3. Using the /api/v2/files/{limsid}/upload endpoint, upload the file.

Attachments

replace file APPLICATION EXAMPLE.py:

Last updated