gpt4 book ai didi

node.js - 使用 Google Functions 和 Node JS 10 复制整个源文件夹而不仅仅是触发器文件

转载 作者:太空宇宙 更新时间:2023-11-04 01:21:52 26 4
gpt4 key购买 nike

需要调整以下代码,以便它复制整个 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}`);
}

最佳答案

您可以调用getFiles()在目录中构建文件列表,然后复制文件。像这样的东西(您可能需要根据您的场景进行修改):

const [ files ] = await storage.bucket(sourceFileBucket).getFiles({
autoPaginate: false,
prefix: sourceFolder
});

files.forEach(file => file.copy( ... ));

关于node.js - 使用 Google Functions 和 Node JS 10 复制整个源文件夹而不仅仅是触发器文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58934727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com