我正在尝试复制指南 Example: image transformation .
我很好,直到我需要打电话:
import * as gcs from '@google-cloud/storage';
import * as functions from 'firebase-functions';
export const onFileChange = functions.storage.object().onFinalize(object => {
// this line throws a TypeScript Error
const destBucket = gcs.bucket(fileBucket);
...
}
Property 'bucket' does not exist on type 'typeof Storage'. Did you mean 'Bucket'?
我也试过:
const destBucket = new gcs.Bucket(new gcs.Storage({}), object.bucket);
这可以编译,但在执行时出现错误:
gcs.Storage is not a constructor
好像API变了,不过我已经更新到最新版本了:
"@google-cloud/storage": "^1.6.0",
"firebase-functions": "^1.0.2",
如何获取 Bucket 的引用以便调用?
destBucket
.file(filePath)
.download({ destination: tempFilePath })
.then(() => { ... })
最佳答案
您是否使用如下配置对象声明了 gcs:
const config = {
projectId: '....',
keyFilename: './.......-adminsdk-0vlsn-34c393497c.json'
};
const storage = require('@google-cloud/storage')(config);
您必须使用在 Firebase 中生成的 service-key.json,如此 Cloud Functions 官方示例中所述https://github.com/firebase/functions-samples/tree/master/generate-thumbnail
“转到 Firebase 控制台,选择齿轮图像 > 项目设置 > 服务帐户,然后单击生成新私钥以下载服务帐户 key JSON 文档。”
此外,错误消息很可能意味着您必须创建存储实例。
关于typescript - 如何在我的云功能中获取对谷歌云存储桶的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50036513/