You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

借助Google Functions与Node.js 10实现复制整个源文件夹而非仅触发文件

Copy Entire GCS Folder on File Trigger (Instead of Single File)

Alright, let's fix your cloud function so it copies the entire folder when any file in that folder triggers the event. Here's the revised code, plus a breakdown of the key changes to make this work:

Modified Working Code

const {Storage} = require('@google-cloud/storage');
// Fixed the path import (original had incorrect destructuring)
const path = require('path');

exports.copyRenders = async (event, context) => {
  const gcsEvent = event;
  const sourcePathOnly = gcsEvent.name;
  const folderToWatch = '... folder on source bucket...';

  // Only process files within our target watch folder
  if (sourcePathOnly.indexOf(folderToWatch) > -1) {
    const storage = new Storage();
    const sourceFileBucket = gcsEvent.bucket;
    // Extract the full parent folder path of the triggered file
    const sourceFolderPath = sourcePathOnly.slice(0, sourcePathOnly.lastIndexOf("/"));
    const destFileBucket = 'trans-test';

    // Fetch all files in the source folder (adjust delimiter if you need subfolders)
    const options = {
      prefix: sourceFolderPath,
      // Optional: Use delimiter to exclude subfolders; remove if you want to copy them too
      delimiter: '/'
    };
    const [files] = await storage.bucket(sourceFileBucket).getFiles(options);

    // Copy all files in parallel, preserving folder structure
    await Promise.all(files.map(file => {
      // Keep the same relative folder structure in the destination
      const relativeFilepath = file.name.slice(sourceFolderPath.length);
      // Match your original target path logic (last two folder segments)
      const destFolderSegments = sourceFolderPath.split('/').slice(-2);
      const destFilePath = `${destFolderSegments.join('/')}${relativeFilepath}`;
      
      return file.copy(storage.bucket(destFileBucket).file(destFilePath));
    }));

    console.log(`Successfully copied all files from ${sourceFolderPath} to bucket ${destFileBucket}`);
  }
  console.log(`Triggered by file: ${sourcePathOnly}`);
}

Key Changes & Explanations

  • Async/Await for Async Operations: Added async to the function and used await for getFiles and Promise.all. This ensures the cloud function doesn't terminate before all copy operations finish (a common pitfall with unhandled async code).
  • Fetch Entire Folder: Used getFiles with prefix: sourceFolderPath to retrieve every file in the folder that triggered the event. The delimiter: '/' option prevents pulling in files from subfolders – remove it if you want to copy nested folders too.
  • Preserve Folder Structure: Calculated the relative path of each file to maintain the same hierarchy in the destination bucket, so files don't get dumped into a single root folder.
  • Fixed Path Import: Corrected the broken path import (original used incorrect destructuring that would throw an error).
  • Parallel Copy: Promise.all runs all copy operations in parallel, making the process more efficient than sequential copies.

Important Notes

  • Ensure your cloud function's service account has the storage.objects.get permission on the source bucket and storage.objects.create on the destination bucket.
  • If you're working with large numbers of files, you might want to add error handling (try/catch blocks) to handle any failed copies gracefully.

内容的提问来源于stack exchange,提问作者Dumpy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:42:32