javascript - 从 Google Cloud Functions (JS) 中运行 Google Storage 命令

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

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

我错过了什么......?

谢谢

最佳答案

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

如果我们查看 Node.js 客户端的 API,我们不会看到任何类似通配符的描述。例如,bucket 对象的 file 函数表示名称为:

The name of the file in this bucket.

如果您想要移动多个文件,您似乎必须执行 getFiles API 调用(请参阅 here )。这将返回一个解析为文件对象数组的 promise 。然后,您可以迭代每个文件对象并对每个文件对象执行 move API 调用(请参阅 here )。

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

关于javascript - 从 Google Cloud Functions (JS) 中运行 Google Storage 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60624174/

相关文章:

javascript - 暂停 JS 打字机

node.js - 如何在页面刷新后保持登录状态?

node.js - 如何在 Cloud Functions for Firebase 中获取服务器时间戳?

firebase - 在同一域上使用 firebase 函数的 oAuth 的跨域状态 cookie 问题

javascript - 什么 javascript 应该放在 $(document).ready(function() 中?

javascript - 在循环中使用 window.location 插入数据时出现问题(GET 方法)

node.js - 使用 sequelize 时 Heroku 超时

Golang Cloud Function 部署失败,第一个路径元素中缺少点

javascript - 为获得最大的缓存性能而对 javascript 进行版本控制的常用方法是什么?

postgresql - 使用 node-postgres 监听查询超时?