An attachment is a file that acts like a property of an object. Attachments are uploaded as temporary files and attached to objects via Actions. Once attached to an object, an attachment is persisted and can be accessed similarly to other properties.
Attachments can be passed directly into Functions as inputs from Actions or accessed as properties on objects. You can also create attachments in Functions. Attachments are managed via the API Gateway and an attachment type is provided in the Python OSDK.
For convenience, the OSDK Attachment type is re-exported from the Python Functions API package and can be imported with the following:
Copied!1
from functions.api import Attachment
To read the raw data from an attachment, use the read
method on the attachment.
The signature for the read
method is as follows:
Copied!1
def read(self) -> BytesIO: ...
BytesIO
is a standard Python type ↗, representing a binary stream.
Within a Python function, you have access to a temporary disk for processing files. This means you can use most Python file processing libraries, including those that require disk access or binary dependencies, unlike when processing Attachments in TypeScript Functions.
Functions can also be used to create attachments and attach them to objects. For attachments created in Functions to be persisted, the Function must make an Ontology edit that links the attachment to an object.
Attachments that are not attached to an object can only be viewed by the uploader and are automatically deleted after a certain period of time.
To create an attachment, use upload
on client.ontology.attachments
from an instantiated FoundryClient
.
The signature for the upload
method is as follows:
Copied!1
def upload(file_path: str, attachment_name: str) -> None: ...
where file_path
is a local file to be uploaded.
The following example demonstrates reading data from attachments, uploading a file, and assigning the resulting attachment to an object:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
from io import BytesIO from functions.api import function, Attachment, OntologyEdit from ontology_sdk import FoundryClient from ontology_sdk.ontology.objects import Aircraft @functions(edits=[Aircraft]) def update_maintenance_log( aircraft: Aircraft, completed_maintenance_log: Attachment ) -> list[OntologyEdit]: ontology_edits = FoundryClient().ontology.edits() maintenance_log_data: BytesIO = aircraft.maintenance_log.read() completed_maintenance_log_data: BytesIO = completed_maintenance_log.read() # Compare the current aircraft logs and completed logs and create a new maintenance log updated_maintenance_log_data: BytesIO = get_updated_maintenance_log( maintenance_log_data, completed_maintenance_log_data ) editable_aircraft = ontology_edits.objects.Aircraft.edit(aircraft) with open("updated-maintenance-log.txt", "wb") as f: f.write(updated_maintenance_log_data.getbuffer()) editable_aircraft.maintenance_log = client.ontology.attachments.upload( "updated-maintenance-log.txt", "my_attachment" ) return ontology_edits.get_edits()