Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

1.6 KB
Raw Blame
import { Readable } from 'node:stream';
import { Property, createAction } from '@activepieces/pieces-framework';
import { amazonS3CombinedAuth, S3AuthProps } from '../auth';
import { resolveS3Client } from '../common';
import { readFileOutputSchema } from '../output-schemas';

export const readFile = createAction({
  auth: amazonS3CombinedAuth,
  name: 'read-file',
  outputSchema: readFileOutputSchema,
  classification: 'READ',
  displayName: 'Read File',
  description: 'Read a file from S3 to use it in other steps',
  audience: 'human',
  aiMetadata: {
    description: 'Downloads a single object from the configured S3 bucket by its full key (path) and makes its contents available as a file for later steps. Use to fetch a known file when you have its exact key. Reading the same key repeatedly returns the same content with no side effect (idempotent).',
    idempotent: true,
  },
  props: {
    key: Property.ShortText({
      displayName: 'File Path',
      description: 'The full path to the file within your S3 bucket (e.g. "documents/report.csv" or "myfile.txt"). This is also called the S3 "key".',
      required: true,
    }),
  },
  async run(context) {
    const authProps: S3AuthProps = context.auth.props;
    const { bucket } = authProps;
    const { key } = context.propsValue;
    const s3 = await resolveS3Client({ authProps, server: context.server });

    const file = await s3.getObject({
      Bucket: bucket,
      Key: key,
    });
    if (!(file.Body instanceof Readable)) {
      throw new Error(`Could not read file ${key} from S3`);
    }
    return await context.files.write({
      fileName: key,
      data: file.Body,
    });
  },
});