TypeScript v1 functions are executed in an environment that has strict memory limits. Exceeding these memory limits can happen quickly when dealing with file data; we recommend only interacting with attachments under 20MB. Python and TypeScript v2 functions have a default limit of 1GB, which can be adjusted in Ontology Manager.
An attachment is a file that acts like an object property. Attachments are uploaded as temporary files and attached to objects using actions. Once attached to an object, an attachment is persisted and can be accessed similarly to other properties.
Attachments can be passed into functions as inputs from actions, or accessed as properties on objects. You can also create and return attachments in functions.
When using Python, attachments are managed using the API Gateway. An attachment type is provided in the Python OSDK.
Below is the import syntax for attachments by language:
Copied!1
import { Attachment } from "@foundry/functions-api";
A read method is provided on attachments to read their raw data. The signature for the method is as follows:
Copied!1 2 3
// Blob is a standard JavaScript type, representing a file-like object of immutable, raw data. // https://developer.mozilla.org/en-US/docs/Web/API/Blob readAsync(): Promise<Blob>;
You may need to use libraries or write your own custom code for handling complex file types. For example, PDFs must be parsed with an appropriate library. Learn more about adding dependencies to functions repositories.
TypeScript v1 functions do not offer filesystem support. Often, dependencies related to parsing file data will rely on the fs
module, which is not available in the functions environment. This restriction may cause fs
module errors during compilation and execution. To work around this restriction, you can introduce a dependency on an in-memory file system (memfs
, for example). Then, alias the dependency under the fs
name.
Below is an example using the NPM dependency memfs
in a package.json
file:
Copied!1
"fs": "npm:memfs@^x.x.x"
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.
TypeScript v2 functions do not support creating attachments.
To create an attachment, use an upload function on the attachment. The signature for the upload function by language is as follows:
Copied!1 2 3 4
import { Attachments, Attachment } from "@foundry/functions-api"; // On Attachments: uploadFile(filename: string, blob: Blob): Promise<Attachment>;
The following example shows the process for 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
import { Attachments, Attachment, OntologyEditFunction } from "@foundry/functions-api"; @OntologyEditFunction() public async updateMaintenanceLog(aircraft: Aircraft): Promise<void> { const aircraftMaintenanceLogData: Blob = await aircraft.maintenanceLog.readAsync(); const completedMaintenanceLogData: Blob = await completedMaintenanceLog.readAsync(); // You will likely need to rely on libraries or custom code to create the `Blob` object, which is // passed as a parameter into the `uploadFile` method. // Compare the current aircraft logs and completed logs and create a new maintenance log. const updatedMaintenanceLogData: Blob; aircraft.maintenanceLog = await Attachments.uploadFile("maintenance-log.txt", updatedMaintenanceLogData); }