【问题标题】:Copy whole source folder rather than just the trigger file using Google Functions & Node JS 10使用 Google Functions & Node JS 10 复制整个源文件夹而不仅仅是触发器文件
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    • 感谢特拉维斯的回复。 await 出现以下错误:“SyntaxError: await is only valid in async function”
    • 您也可以根据文档向getFiles 发送回调。
    【解决方案2】:

    可以复制一个目录前缀的所有文件

    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]));
    });
    
    

    请注意,每次触发您的函数时,都会复制整个前缀匹配项。如果你在同一个桶中复制,你可以创建一个指数无限循环!

    注意:从数学上讲,我不知道无限的东西是否可以是指数的!

    【讨论】:

    • 对不起,如果这是一个真正的菜鸟问题纪尧姆,但四处搜索,所有结果都指向天蓝色,只是无法掌握如何“复制 blob”。我需要定义“blob”吗?
    • 我很惭愧。我用 Python 而不是 NodeJS 回答了你。我更新了我的答案。再次,我很抱歉。
    • 哈哈,我以为我要疯了。谢谢纪尧姆。我想我没有正确地将它添加到原始代码中。介意在我原来的问题中查看修改后的版本并检查我确实把它放在了正确的位置吗?
    • 感谢 Guillame,刚刚尝试过,但仍然出现错误:textPayload:“TypeError:存储不是构造函数。明天我有时间再看看,让你知道。
    • 包文件已恢复,因此只需更新它,不再出现存储错误。我收到以下错误,但不确定问题出在哪里? textPayload: "TypeError: storage.bucket(...).getFiles 不是函数或其返回值不可迭代
    猜你喜欢
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多