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

2.2 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { streamUtils } from '@activepieces/pieces-common';
import mime from 'mime-types';
import { drive as googleDrive } from '@googleapis/drive';
import { googleDriveAuth, createGoogleClient } from '../auth';
import { common } from '../common';
import { uploadGdriveFileActionOutputSchema } from '../output-schemas';

export const googleDriveUploadFile = createAction({
  auth: googleDriveAuth,
  name: 'upload_gdrive_file',
  classification: 'WRITE',
  description: 'Upload a file to Google Drive.',
  audience: 'human',
  aiMetadata: { description: 'Uploads a binary file (from a URL or base64 input) into Google Drive, optionally inside a parent folder. Use to store an existing file or attachment in Drive; the MIME type is inferred from the file extension. Not idempotent: each call creates a new file.', idempotent: false },
  displayName: 'Upload File',
  props: {
    fileName: Property.ShortText({
      displayName: 'File Name',
      description: 'Name shown in Drive, including the extension.',
      required: true,
      placeholder: 'photo.png',
    }),
    file: Property.File({
      displayName: 'File',
      description: 'The file to upload, from an earlier step or a URL.',
      required: true,
      streaming: true,
    }),
    parentFolder: common.properties.parentFolder,
    include_team_drives: common.properties.include_team_drives,
  },
  outputSchema: uploadGdriveFileActionOutputSchema,
  async run(context) {
    const fileData = context.propsValue.file;
    const { body } = streamUtils.toStreamingBody(fileData);
    const mimeType = mime.lookup(fileData.extension ?? '') || 'application/octet-stream';

    const authClient = await createGoogleClient(context.auth);
    const drive = googleDrive({ version: 'v3', auth: authClient });

    const response = await drive.files.create({
      requestBody: {
        name: context.propsValue.fileName,
        ...(context.propsValue.parentFolder
          ? { parents: [context.propsValue.parentFolder] }
          : {}),
      },
      media: {
        mimeType,
        body,
      },
      supportsAllDrives: context.propsValue.include_team_drives ?? false,
      fields: 'id, name, mimeType, kind',
    });

    return response.data;
  },
});