Mercurial > hg > orthanc-book
changeset 748:a296fe06fd86
Implementing a custom storage area in Python
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 12 Aug 2021 17:51:54 +0200 |
parents | 56d48f6e52cc |
children | 4ae741e351ab |
files | Sphinx/source/plugins/python.rst Sphinx/source/plugins/python/storage-area.py |
diffstat | 2 files changed, 59 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/Sphinx/source/plugins/python.rst Mon Aug 09 18:29:38 2021 +0200 +++ b/Sphinx/source/plugins/python.rst Thu Aug 12 17:51:54 2021 +0200 @@ -715,6 +715,46 @@ :language: python +.. _python_storage_area: + +Implementing a custom storage area (new in 3.3) +............................................... + +Starting with release 3.3 of the Python plugin, it is possible to +replace the built-in storage area of Orthanc (that writes +:ref:`attachments <metadata>` onto the filesystem in the +``OrthancStorage`` folder by default), by providing 3 Python callbacks +to the ``orthanc.RegisterStorageArea()`` function: + +* The first callback indicates how to **create** an attachment into + the storage area. + +* The second callback indicates how to **read** an attachment from the + storage area. + +* The third callback indicates how to **remove** an attachment out of + the storage area. + +This feature can be used to quickly and easily interface Orthanc with +any `object-based storage +<https://en.wikipedia.org/wiki/Object_storage>`__ technology available +in Python (such as `Ceph +<https://en.wikipedia.org/wiki/Ceph_(software)>`__ or AWS S3-like +tools). The performance will not be as good as a C/C++ native plugin +(cf. the :ref:`cloud storage <object-storage>`, the :ref:`PostgreSQL +<postgresql>` and the :ref:`MySQL <mysql>` plugins), but it can be +used for prototyping or for basic setups. + +Here is a full, self-explaining sample: + +.. literalinclude:: python/storage-area.py + :language: python + +The ``contentType`` can be used to apply a special treatment to some +types of attachments (typically, DICOM instances). This parameter +takes its values from the ``orthanc.ContentType`` enumeration. + + Performance and concurrency ---------------------------
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sphinx/source/plugins/python/storage-area.py Thu Aug 12 17:51:54 2021 +0200 @@ -0,0 +1,19 @@ +import orthanc +import os + +def GetPath(uuid, contentType): + # Returns the path where to store the given attachment + return 'attachment-%d-%s' % (contentType, uuid) + +def OnCreate(uuid, contentType, data): + with open(GetPath(uuid, contentType), 'wb') as f: + f.write(data) + +def OnRead(uuid, contentType): + with open(GetPath(uuid, contentType), 'rb') as f: + return f.read() + +def OnRemove(uuid, contentType): + os.remove(GetPath(uuid, contentType)) + +orthanc.RegisterStorageArea(OnCreate, OnRead, OnRemove)