How can I load attachment >20Mb in a Foundry Function?

I want to load an attachment in memory that is fairly large (e.g. a PDF of >20 Mb) in memory of a foundry functions that operates on objects.

When trying to load in Typescript v1 functions, I sometimes get timeouts. How can I load those attachments in memory still ?

1 Like

Typescript v1 might have issues with files above 20Mb.


See https://www.palantir.com/docs/foundry/functions/api-attachments

One trivial solution is to use Typescript v2 functions (currently experimental at time of writing, which means that you might have compile-time breaks to handle whenever you upgrade the repository, for example).

To do so, create a new code repositoy and select “Functions” > “Typescript v2”

Here is an example code snippets that loads the attachment in memory and print the number of pages of the document.

import { Osdk} from "@osdk/client";
import { PdfMediaObjectExample } from "@osdk/generated";
import { PDFDocument, rgb } from 'pdf-lib';

async function getAttachmentContent(oneObject: Osdk.Instance<PdfMediaObjectExample>): Promise<string> {
    const metadata = await oneObject.attachment?.fetchMetadata();
    // metadata?.filename
    // metadata?.mediaType
    // metadata?.rid
    // metadata?.sizeBytes

    const response = await oneObject.attachment?.fetchContents();
    // response?.ok
    // response?.text
    // response?.arrayBuffer
    // response?.body
    // response?.bodyUsed
    // ...
    const blob = await response?.blob()

    // Convert the Blob to an ArrayBuffer
    const arrayBuffer = await blob!.arrayBuffer();

    // Convert the ArrayBuffer to a Uint8Array
    const pdfBytes = new Uint8Array(arrayBuffer);

    // Load the existing PDF document
    const pdfDoc = await PDFDocument.load(pdfBytes);

    // Get the first page of the document
    const pages = pdfDoc.getPages();
    const firstPage = pages[0];

    // Extract some information or perform operations on the first page
    // For instance, you could return the number of pages or some text
    const numberOfPages = pages.length;
    return `PDF loaded in memory with ${numberOfPages} pages.`;

}

export default getAttachmentContent;

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.