Skip to content

Support GoogleAIFileManager.uploadFile in Serverless Environments (Cloudflare Workers, AWS Lambda, etc.) #364

Open
@adilsonx

Description

@adilsonx

Description of the feature request

The current implementation of GoogleAIFileManager.uploadFile is tightly coupled to Node.js because it directly calls fs.readFileSync(filePath). This makes it incompatible with serverless environments such as Cloudflare Workers, AWS Lambda, Vercel Edge Functions, where the fs module is unavailable.

A similar issue was discussed in [#327](https://github.com/your-repo/issues/327), but this proposal focuses specifically on serverless backends rather than browser support.

Problem

I'm developing an AI application using Gemini that runs on Cloudflare Workers, and I need to upload big files using the GoogleAIFileManager API. However, since fs is not available, the SDK cannot be used in this environment.

Example usage that currently fails in Cloudflare Workers:

const fileManager = new GoogleAIFileManager(apiKey);
await fileManager.uploadFile(filePath, { mimeType: "text/plain" }); 

Since Workers do not have access to a file system, the SDK's current implementation is unusable in serverless runtimes.

Proposed Solutions

To support serverless environments, I suggest the following:

  1. Support for ArrayBuffer input

    • This would allow direct uploads from memory, making it compatible with Workers and other serverless runtimes.
    • However, platforms like Cloudflare Workers have a 128MB memory limit, which restricts file sizes.
  2. Support for Public URL references (Preferred Solution)

    • Instead of requiring a local file path, allow direct uploads from URLs:
      await fileManager.uploadFile("https://example.com/myfile.pdf", { 
        mimeType: "application/pdf"
      });
    • This removes memory constraints and enables processing of much larger files in a serverless-first approach.

Current Workaround & Limitations

Right now, my only option is to encode files as base64 and include them in API requests, which:

  • Limits file size to 20MB.
  • Prevents the use of the more optimized Files API.
  • Reduces efficiency and increases request payload size.

Why This Matters

Many modern applications rely on serverless backends, and having a way to upload files without relying on fs would make the SDK significantly more flexible.

Would love to hear your thoughts on this! Thanks for your time.

What problem are you trying to solve with this feature?

I've tried creating a custom implementation that accepts ArrayBuffer inputs:

class CloudflareFileManager {
  async uploadFile(fileBuffer: ArrayBuffer, metadata: FileMetadata) {
    // Custom implementation with multipart requests
  }
}

However, I'm facing authentication issues with the API and struggling to replicate the exact format expected by Google's endpoints.

Metadata

Metadata

Assignees

No one assigned

    Labels

    component:js sdkIssue/PR related to JavaScript SDKp3status:triagedIssue/PR triaged to the corresponding sub-teamtype:feature requestNew feature request/enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions