借助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
asyncto the function and usedawaitforgetFilesandPromise.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
getFileswithprefix: sourceFolderPathto retrieve every file in the folder that triggered the event. Thedelimiter: '/'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
pathimport (original used incorrect destructuring that would throw an error). - Parallel Copy:
Promise.allruns 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.getpermission on the source bucket andstorage.objects.createon 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
相关产品推荐
相关产品推荐

