从谷歌云功能(JS)中运行谷歌存储命令



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

这是云功能代码:

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

我错过了什么。。。?

感谢

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

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

此存储桶中文件的名称。

如果要移动多个文件,则需要执行getFilesAPI调用(请参见此处(。这将返回一个解析为文件对象数组的promise。然后,您可以对每个文件对象进行迭代,并对每个对象执行moveAPI调用(请参见此处(。

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

最新更新