【问题标题】:Running Google Storage Command from within Google Cloud Functions (JS)从 Google Cloud Functions (JS) 中运行 Google Storage Command
【发布时间】:2020-03-10 18:54:13
【问题描述】:

我正在使用 Cloud Functions 将 GCS 中一个文件夹的全部内容移动到同一存储桶中的另一个文件夹中。我正在使用 Javascript。 尽管存储桶和源对象存在,但我一直收到对象不存在的错误。

这是云函数代码:

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function moveFile() {
  // Moves the file within the bucket
  await storage
    .bucket("my-bucket")
    .file("folder/source/**")
    .move("folder/target/**");

  console.log(
    `gs://${bucketName}/${srcFilename} moved to gs://${bucketName}/${destFilename}.`
  );
}
exports.moveContent = (req, res) => {
   moveFile().catch(console.error);
  res.send("done")
}

这是package.json的来源

{
  "name": "move-content",
  "version": "0.0.1",
  "dependencies": {
    "googleapis": "37.1.0",
    "@google-cloud/storage": "^4.5.0"
  }
}

这是我得到的错误(来自 Google 日志):

move-content 3m89p16m58bb { Error: file#copy failed with an error - No such object: my-bucket/folder/source/** at new ApiError (/srv/node_modules/@google-cloud/common/build/src/util.js:58:15) at Util.parseHttpRespBody (/srv/node_modules/@google-cloud/common/build/src/util.js:193:38) at Util.handleResp
...

我错过了什么...?

谢谢

【问题讨论】:

  • 你要移动多少个文件?
  • 可以是几十到几百
  • 如果您因为文件太多而出现超时问题,请不要犹豫再发一个问题!

标签: javascript node.js google-cloud-functions google-cloud-storage


【解决方案1】:

在您的示例中,您似乎使用了通配符。我相信这些通配符适用于名为gsutil 的工具。 Here 是一篇关于 gsutil 通配符的文章。

如果我们查看 Node.js 客户端的 API,我们看不到任何类似通配符的描述。比如bucket对象的file函数说名字是:

此存储桶中文件的名称。

如果您想移动多个文件,看起来您必须执行getFiles API 调用(请参阅here)。这将返回一个解析为文件对象数组的承诺。然后,您可以遍历每个文件对象并对每个文件对象执行move API 调用(请参阅here)。

这个故事似乎与您收到的错误消息一致。

【讨论】:

  • 顺便说一句,您知道是否可以在bigquery.dataset.table.load 函数中使用通配符来并行加载多个文件?
  • 对于您的 BQ 问题...查看此处的 API ...googleapis.dev/nodejs/bigquery/latest/Table.html#load 似乎只显示了一个文件的异步加载。但是,我没有看到任何 cmets 会说您无法根据需要运行对该函数的尽可能多的调用(每个源文件一个)。由于这些是非阻塞的......我们应该没问题。
猜你喜欢
  • 2017-09-30
  • 2019-07-11
  • 2018-04-22
  • 2020-03-07
  • 2020-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多