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!1from 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!1def 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!1def 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!1from io import BytesIO 2 3from functions.api import function, Attachment, OntologyEdit 4from ontology_sdk import FoundryClient 5from ontology_sdk.ontology.objects import Aircraft 6 7 8@functions(edits=[Aircraft]) 9def update_maintenance_log( 10 aircraft: Aircraft, 11 completed_maintenance_log: Attachment 12) -> list[OntologyEdit]: 13 client = FoundryClient() 14 ontology_edits = client.ontology.edits() 15 16 maintenance_log_data: BytesIO = aircraft.maintenance_log.read() 17 completed_maintenance_log_data: BytesIO = completed_maintenance_log.read() 18 19 # Compare the current aircraft logs and completed logs and create a new maintenance log 20 updated_maintenance_log_data: BytesIO = get_updated_maintenance_log( 21 maintenance_log_data, 22 completed_maintenance_log_data 23 ) 24 25 editable_aircraft = ontology_edits.objects.Aircraft.edit(aircraft) 26 27 with open("updated-maintenance-log.txt", "wb") as f: 28 f.write(updated_maintenance_log_data.getbuffer()) 29 30 editable_aircraft.maintenance_log = client.ontology.attachments.upload( 31 "updated-maintenance-log.txt", 32 "my_attachment" 33 ) 34 35 return ontology_edits.get_edits()