简体   繁体   English

如何从 Cloud Functions for Firebase 文件夹读取证书文件

[英]How to read cert files from Cloud Functions for Firebase Folder

I am trying to read a file under certs folder as shown below:我正在尝试读取 certs 文件夹下的文件,如下所示:

-functions
 |
--certs
  |
---idp-public-cert.perm

Here is the code I am using to read the file:这是我用来读取文件的代码:

fs.readFileSync(path.join(__dirname, 'certs/idp-public-cert.pem')) . fs.readFileSync(path.join(__dirname, 'certs/idp-public-cert.pem'))

When I run the function, I get the following errors:当我运行该函数时,出现以下错误:

Error: ENOENT: no such file or directory, open '/srv/lib/certs/idp-public-cert.pem'

I would appreciate any crew on how to do this.我将不胜感激任何工作人员如何做到这一点。 Thanks in advance.提前致谢。

Just read the file using the relative path certs/idp-public-cert.pem .只需使用相对路径certs/idp-public-cert.pem读取文件。 All relative paths will be interpreted relative to your deployed functions folder.所有相对路径都将相对于您部署的函数文件夹进行解释。 Don't try to build a full path to the file, as you shouldn't assume where your code deployment is effectively mounted in the server instance.不要尝试构建文件的完整路径,因为您不应该假设您的代码部署在服务器实例中有效安装的位置。

I found that specifying path with "./" or "/" is iffy - because it takes a relative patch from somewhere, maybe OLDPWD or PWD, not sure how it messes up But I found that the right way to do fix is to add __dirname to the path.我发现用“./”或“/”指定路径是不确定的 - 因为它需要从某个地方获取一个相对补丁,可能是 OLDPWD 或 PWD,不确定它是如何搞砸的但我发现修复的正确方法是添加__dirname 到路径。 __dirname will give the path of the functions directory. __dirname 将给出函数目录的路径。 so if your function is in /home/username/firebase/functions/routes, then __dirname will show you that.所以如果你的函数在 /home/username/firebase/functions/routes 中,那么 __dirname 会告诉你。 So if you have a file /home/username/firebase/abc/, then you would have to specify: fs.readFileSync(__dirname+"../abc/"因此,如果您有一个文件 /home/username/firebase/abc/,那么您必须指定:fs.readFileSync(__dirname+"../abc/"

Hope this saves the time of others!!希望这可以节省其他人的时间!! I wasted so much time on this, it was quite frustrating, and was kicking myself for not reading the doc!我在这上面浪费了很多时间,这很令人沮丧,并且因为没有阅读文档而自责!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM