【发布时间】:2020-04-28 22:11:21
【问题描述】:
我正在尝试检索上传的新文件的下载 url,以便将其写入我的数据库。 我关注了this answer 和official docs,但我的函数出现错误。
这些是我的导入:
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import { FieldValue } from "@google-cloud/firestore";
import { _namespaceWithOptions } from "firebase-functions/lib/providers/firestore";
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const defaultStorage = admin.storage();
这是我目前想要的云功能:
exports.writeFileToDatabase = functions.storage
.object()
.onFinalize(object => {
const bucket = defaultStorage.bucket();
const file = bucket.file(object.name as string);
const options = {
action: 'read',
expires: '03-17-2025'
};
return file.getSignedUrl(options)
.then(results => {
const url = results[0];
console.log(`The signed url is ${url}.`);
return true;
})
});
但是当将options 传递给getSignedUrl 时,我得到了这个错误:
Argument of type '{ action: string; expires: string; }' is not assignable to parameter of type 'GetSignedUrlConfig'
我在results 上也收到错误消息:
Parameter 'results' implicitly has an 'any' type
与我用作参考的示例不同,我看不出我在做什么。
【问题讨论】:
-
您能否编辑问题以准确显示您如何在此处导入或使用模块?尤其是 Cloud Storage 或 Firebase Admin SDK。您的项目的 TypeScript 配置也可能存在问题。您不必具体说明事物的类型 - TypeScript 应该都能理解它们。
-
@DougStevenson 我已经编辑了我的问题并添加了我的导入。但是 - 我实际上最终解决了这个问题,我不确定它为什么会有所作为(我的解决方案)。如果您了解原因,将不胜感激。
标签: typescript firebase google-cloud-functions firebase-storage