我正在尝试返回异步匿名函数的内容参数。内容参数填充在匿名函数块中,但返回时它是未定义的。
我尝试等待整个事情没有效果:
const file =
await storage.bucket(bucketName).file("myImage.jpg").download(async
function(err, contents) {
return contents;
});
<file is undefined>
我也尝试等待内容:
return await contents;
,结果相同。
这是在 GCP 云函数中,但我确信我只是搞砸了这里的 JS。
提前感谢您的指导。
您似乎没有正确使用 API。如果您要使用
download()
返回的 Promise,则无需传递回调函数。
如果要将存储对象的内容下载到内存中,其工作原理如下:
const contents = await storage.bucket(bucketName).file(fileName).download();
console.log(
`Contents of gs://${bucketName}/${fileName} are ${contents.toString()}.`
);
这直接取自来自 Google 的示例代码: