📁 Upload & rename videos to Google Drive via Apps Script from URL

775 views · 📁 File Management

Description

📄 Google Script Workflow: Upload File from URL to Google Drive (via n8n)

🔧 Purpose:

This lightweight Google Apps Script acts as a server endpoint that receives a file URL (from n8n), downloads the file, uploads it to your specified Google Drive folder, and responds with the file’s metadata (like Drive file ID and URL). This is useful for large video/audio files that n8n cannot handle directly via HTTP Download nodes.


🚀 Setup Steps:

1. Create a New Script Project


2. Paste the Script Code

Replace the default Code.gs content with the following (your custom script):

function doPost(e) {
  const SECRET_KEY = 'your-strong-secret-here'; // Set your secret key here

  try {
    const data = JSON.parse(e.postData.contents);

    // 🔒 Check for correct secret key
    if (!data.secret || data.secret !== SECRET_KEY) {
      return ContentService.createTextOutput("Unauthorized")
        .setMimeType(ContentService.MimeType.TEXT);
    }

    const videoUrl = data.videoUrl;
    const folderId = 'YOUR_FOLDER_ID_HERE'; // Replace with your target folder ID
    const folder = DriveApp.getFolderById(folderId);

    const response = UrlFetchApp.fetch(videoUrl);
    const blob = response.getBlob();
    const file = folder.createFile(blob);
    file.setName('uploaded_video.mp4'); // You can customize the name

    return ContentService.createTextOutput(file.getUrl())
      .setMimeType(ContentService.MimeType.TEXT);
  } catch (err) {
    return ContentService.createTextOutput("Error: " + err.message)
      .setMimeType(ContentService.MimeType.TEXT);
  }
}

3. Generate & Set Up Secret Key

To allow authorized post requests to your script only, we need to generate a secret key from aany reliable key generator.

  const SECRET_KEY = 'your-strong-secret-here'; // Set your secret key here;

4. Replace Folder ID in Code


5. Set Up Deployment as Web App


📤 How to Use in n8n

1. HTTP Request Node

{
  "videoUrl": "https://example.com/path/to/your.mp4",
  "secret": "your-strong-secret-here"
}

2. Rename Node


🔗 Nodes Used

HTTP Request, Google Drive

📥 Import

Download workflow.json and import into n8n: Workflow menu → Import from File

📖 Importing guide · 🔑 Credential setup