如何在功能中包括存储服务帐户密钥 [英] How to include storage service account key in function

查看:98
本文介绍了如何在功能中包括存储服务帐户密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将服务帐户密钥包含到我的存储功能中,以便能够通过遵循此处的过时示例来获取长期签名的URL

I'm trying to include service account key into my storage function to be able to get long lived signed url by following out of date example here

https://github .com/firebase/functions-samples/blob/b404482342906ee1b46dddb4c75667685ab098a1/generate-thumbnail/functions/index.js#L21

我已经从IAM下载了JSON格式的密钥.我试图将其保存在我的功能旁边

I have downloaded my key from IAM which is in JSON format. I have tried to save it right next to my function

-functions/storage/resizeProfileImg.js

-functions/storage/service-account-credentials.json

-functions/index.js

-functions/admin.js

其中resizeProfileImg.js是我的函数,并且这样调用

where resizeProfileImg.js is my function and call it like this

const { Storage } = require('@google-cloud/storage');
const storage = new Storage({ projectId: projectId ,keyFilename: './service-account-credentials.json'})

但是在部署后,当函数被触发时,我得到了一个错误

but after deployment when the function is triggered then I get an error

错误:ENOENT:没有此类文件或目录,请打开"/srv/service-account-credentials.json"

我甚至试图像这样将其添加为常量

I have even tried to add it in constant like this

const serviceAccountCredentials = require('./accountKey/service-account-credentials.json')

const { Storage } = require('@google-cloud/storage');
const storage = new Storage({ projectId: projectId ,keyFilename: serviceAccountCredentials})

但是我得到一个错误

TypeError:路径必须是字符串.已收到{类型:"service_account",...

任何想法如何正确执行此操作

Any idea how to do this properly

推荐答案

在Cloud Functions中,当前目录.不在您的源文件所在的位置.这是功能文件夹的部署位置.由于您的凭据文件位于名为存储"的子目录中,因此您需要在路径中使用该文件.

In Cloud Functions, the current directory . isn't where your source file is located. It's where the functions folder was deployed. Since your credentials file is in a subdirectory called "storage", you will need to use that in the path.

const serviceAccountCredentials = require('./storage/service-account-credentials.json')

这篇关于如何在功能中包括存储服务帐户密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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