首页 > 解决方案 > 从 Google Cloud Functions (JS) 中运行 Google Storage Command

问题描述

我正在使用 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"
  }
}

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

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
...

我错过了什么……?

谢谢

标签: javascriptnode.jsgoogle-cloud-functionsgoogle-cloud-storage

解决方案


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

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

此存储桶中文件的名称。

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

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


推荐阅读