Download Google Drive File Using Cloudflare Worker (Single File ID)

In this tutorial, we will create a Cloudflare Worker that downloads a Google Drive file using a single file ID. The worker will fetch the file metadata, get the original file name and type, and then stream the file directly to the user.

This method is useful for:

  • File sharing websites
  • Google Drive direct download links
  • Proxy download systems
  • Automation tools
  • Cloudflare Worker file delivery

How This Worker Works

  1. User requests the Worker URL
  2. Worker reads the file ID from the URL
  3. Worker fetches file metadata from Google Drive API
  4. Worker gets original file name and type
  5. Worker streams the file to the user

Worker URL Example


https://your-worker.workers.dev/?id=FILE_ID

Replace FILE_ID with your Google Drive file ID.


Complete Cloudflare Worker Code (Single ID)


addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
});

async function fetchFile(fileId, apiKey) {

  const endpoint =
    `https://www.googleapis.com/drive/v3/files/${fileId}?supportsAllDrives=true&key=${apiKey}`;

  const metadataResponse = await fetch(endpoint);

  if (metadataResponse.status !== 200) {
    return null;
  }

  const fileMetadata = await metadataResponse.json();

  const originalFileName = fileMetadata.name;
  const originalFileType = fileMetadata.mimeType;

  const downloadUrl =
    `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media&supportsAllDrives=true&key=${apiKey}`;

  const fileResponse = await fetch(downloadUrl);

  if (fileResponse.status !== 200) {
    return null;
  }

  return new Response(fileResponse.body, {
    status: fileResponse.status,
    headers: {
      'Content-Type': originalFileType,
      'Content-Disposition':
        `attachment; filename="${originalFileName}"`
    }
  });
}

async function handleRequest(request) {

  const url = new URL(request.url);

  const id = url.searchParams.get('id');

  if (!id) {
    return new Response(
      'File ID not provided',
      { status: 400 }
    );
  }

  const apiKey =
    'YOUR_GOOGLE_API_KEY';

  const response =
    await fetchFile(id, apiKey);

  if (response) {
    return response;
  }

  return new Response(
    'File not accessible or does not exist',
    { status: 404 }
  );
}

Example Request


https://worker.example.com/?id=1AbCdEfGhIjKlMnOpQrStUvWxYz

Required Setup

1. Create Google Cloud Project

  • Go to Google Cloud Console
  • Create new project
  • Enable Google Drive API
  • Create API Key

2. Deploy Cloudflare Worker

  • Open Cloudflare Dashboard
  • Go to Workers
  • Create new Worker
  • Paste the code
  • Deploy

Features

  • Single file ID support
  • Automatic file name detection
  • Streams file without saving to memory
  • Supports large files
  • Works with shared drives

Common Errors

File ID not provided


File ID not provided
This means:
  • You did not pass the id parameter
Correct:

? id=FILE_ID

File not accessible


File not accessible or does not exist
This means:
  • File is private
  • Wrong file ID
  • Drive API not enabled

Use Cases

  • Google Drive file proxy
  • Direct download system
  • File sharing website backend
  • Cloudflare Worker CDN delivery
  • Secure file distribution

Conclusion

In this tutorial, we created a Cloudflare Worker that downloads a Google Drive file using a single file ID. This method is fast, reliable, and perfect for file sharing platforms and automation systems.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top