【发布时间】:2020-03-15 00:18:36
【问题描述】:
需要调整以下代码,使其复制整个 sourceFolder 而不仅仅是事件文件本身,但不确定如何执行此操作。
const {Storage} = require('@google-cloud/storage');
const {path} = require('path');
exports.copyRenders = (event, context) => {
const gcsEvent = event;
const sourcePathOnly = gcsEvent.name
const folderToWatch = ' ... folder on source bucket... '
// Process only if it's in the correct folder
if (sourcePathOnly.indexOf(folderToWatch) > -1) {
const storage = new Storage();
const sourceFileBucket = gcsEvent.bucket
const sourceFolder = sourcePathOnly.split('/').slice(-2)
const destFileBucket = 'trans-test'
storage
.bucket(sourceFileBucket)
.file(sourcePathOnly)
.copy(storage.bucket(destFileBucket).file(sourceFolder[0] + '/' +
sourceFolder[1]));
}
console.log(`Processing file: ${sourcePathOnly}`);
}
使用下面答案中的新代码:
const {Storage} = require('@google-cloud/storage');
const {path} = require('path');
exports.copyRenders = (event, context) => {
const gcsEvent = event;
const sourcePathOnly = gcsEvent.name
const folderToWatch = ' ... folder on source bucket... '
// Process only if it's in the correct folder
if (sourcePathOnly.indexOf(folderToWatch) > -1) {
const storage = new Storage();
const sourceFileBucket = gcsEvent.bucket
const sourceFolder = sourcePathOnly.split('/').slice(-2)
const destFileBucket = 'trans-test'
const options = {
// Get the source path without the file name)
prefix: sourcePathOnly.slice(0,sourcePathOnly.lastIndexOf("/")),
};
const [files] = storage.bucket(sourceFileBucket).getFiles(options);
files.forEach(file => {
file.copy(storage.bucket(destFileBucket).file(sourceFolder[0] + '/' + sourceFolder[1]));
});
}
console.log(`Processing file: ${sourcePathOnly}`);
}
【问题讨论】:
标签: node.js google-cloud-platform google-cloud-functions google-cloud-storage