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