部署云功能时如何通过证书密钥文件? [英] How to pass certificate key file when deploying cloud function?

查看:43
本文介绍了部署云功能时如何通过证书密钥文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的TypeScript文件中,我具有以下内容:

In my TypeScript file, I have the following:

import * as admin from 'firebase-admin'
import * as functions from 'firebase-functions'
const serviceAccountKey = "serviceAccountKey.json"
const databaseURL = "https://blahblah.firebaseio.com"

admin.initializeApp({
    credential: admin.credential.cert(serviceAccountKey),
    databaseURL: databaseURL
});

我的函数在模拟器中运行良好(只有在成功传递json密钥的情况下才能发生),但是当我尝试部署它们时,出现以下错误:

My functions run fine in the emulator (which can only happen if the json key is successfully passed) but when I try to deploy them, I get the following error:

Error: Error occurred while parsing your function triggers.
Error: Failed to parse certificate key file: Error: ENOENT: no such file or directory, open 'serviceAccountKey.json'

我尝试将json文件移动到根文件夹,lib文件夹(index.js文件所在的位置),functions文件夹,但没有任何运气.部署时如何传递密钥?

I've tried moving the json file to the root folder, the lib folder (where the index.js file resides), the functions folder, without any luck. How do I pass the key when deploying?

推荐答案

当您使用Firebase CLI将内容部署到Cloud Functions时,它将上传您的functions文件夹的所有内容(node_modules除外,它将使用其中的内容进行重建)您的package.json).该文件夹将成为您部署的根目录.函数执行时,将准备好使用所有这些文件,但是您必须使用相对路径正确地引用它们.

When you deploy content to Cloud Functions using the Firebase CLI, it will upload the entire contents of your functions folder (except node_modules, which it will rebuild using the contents of your package.json). That folder will become the root of your deployment. When your function executes, it will have all of those files ready for use, but you will have to refer to them correctly using relative paths.

以此为视角,我可以看到您的serviceAccount.json和已编译的index.js都在一个名为"lib"的文件夹中.如果您希望index.js中的代码能够从同一文件夹中的其他文件中加载JSON,您要做的就是使用相对路径引用它,如下所示:

Using that as perspective, I can see that your serviceAccount.json and compiled index.js are all in a folder called "lib". If you want code in index.js to be able to load JSON out of other files in the same folder, what you'll have to do is refer to it using a relative path, like this:

const serviceAccountKey = "./serviceAccountKey.json"

请注意路径开头的"./".告诉节点您要引用与当前源文件位于同一文件夹中的文件.

Note the "./" at the start of the path. That's telling node that you want to refer to a file that's in the same folder as the current source file.

我将提出一个建议:对于TypeScript项目,lib通常保留给编译后的代码.我会避免将您的服务帐户文件放在其中,因为可以在全新构建过程中删除该文件夹.最好将其放在部署的根目录中,并将其称为"../serviceAccount.json",这是我的惯常做法.

I'll make one recommendation: For TypeScript projects, lib is typically reserved for compiled code. I would avoid putting your service account file in there, since that folder could be removed during clean build. It might be better to put it at the root of the deployment and refer to it as "../serviceAccount.json", which is my usual practice.

这篇关于部署云功能时如何通过证书密钥文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆