Google云端存储的Firebase云端functionAPI错误

随着Firebase云端function的推出,我们正在考虑将当前的一些node.js服务器端代码移到云端function。 我遇到的一个问题是将文件从GCS存储区下载到磁盘上的临时文件,然后通过电子邮件将其作为附件(使用mailgun-js)发送。

导致我悲伤的那段代码是:

return mkdirp(tempLocalDir).then(() => { const bucket = gcs.bucket(gcsBucket); const tempFilePath = tempLocalDir + gcsFile; return bucket.file(gcsFile).download({ destination: tempFilePath }).then(() => { console.log('File downloaded locally to', tempFilePath); var messageSubject = "Test"; var messageBody = "Test with attach"; var mailgunData = { from: , to: agentEmail, subject: messageSubject, html: messageBody, attachment: tempFilePath, }; mailgunAgent.messages().send(mailgunData, function (error, body) { console.log(body); }); }); }); 

我在函数日志中得到的错误消息是:

 ApiError: Forbidden at Object.parseHttpRespMessage (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:156:33) at Object.handleResp (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:131:18) at Duplexify.<anonymous> (/user_code/node_modules/@google-cloud/storage/src/file.js:724:21) at emitOne (events.js:96:13) at Duplexify.emit (events.js:188:7) at emitOne (events.js:96:13) at DestroyableTransform.emit (events.js:188:7) at emitOne (events.js:96:13) at Request.emit (events.js:188:7) at Request.<anonymous> (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1108:14) 

我已经能够使用请求将文件下载到磁盘上的/ tmp /文件夹,这将是后备选项,但是我真的想要尽可能使用GCS工具。 我“认为”这是与GCS的authentication错误,但我不知道如何跟踪。 在云服务中,我需要在GCS中使用.config()而不是在Firebase中使用不同的身份validation参数吗? 如果是这样,我怎么input它们? 我们的GCS存储桶和项目在Firebase存储的引入之前,但是我们已经成功地将其与我们的服务器上运行的节点function一起使用。

在此先感谢,扎克

求解:终于解决了上面的问题。 问题是@ google-cloud / storage auth需要GCS项目ID作为ID,但Firebase admin-sdk密钥文件,而不是GCS项目密钥文件。 使用Firebase项目ID作为GCS身份validation中ID与Firebase密钥文件也失败。

_(ツ)_ /¯

在我们的项目设置中可能会是一个奇怪的行为,但认为它与更新与什么解决了我们的问题有关。

使用上面grll提供的格式的configuration是:

 const gcs = require('@google-cloud/storage'); const storageClient = gcs({ projectId: "this is the GCS project ID, keyFilename: 'this is the firebase admin-sdk keyFilename.json' }); const bucket = storageClient.bucket(gcsBucket); 

解决此问题的另一种方法是不附加projectId或keyFilename:

  1. 转到您的项目的Firebase控制台
  2. 设置 – >权限
  3. IAM选项卡应该打开
  4. 现在,为“存储pipe理员”angular色提供您的“App Engine默认服务帐户”和“Google API服务帐户”权限
  5. 应该现在好了!

您需要确定才能使用谷歌云存储API。 或者任何其他API的方式。

一种方法是到谷歌云平台上的APIlogin页面: 在这里下载相应的服务帐户密钥作为JSON文件。 然后把这个文件放到你的firebase项目里的函数文件夹中。

最后而不是使用:
const bucket = gcs.bucket(gcsBucket);

你可以这样称呼:

 const gcs = require('@google-cloud/storage'); 

const storageClient = gcs({ projectId: projectId, keyFilename: './keyFilename.json' }); const bucket = storageClient.bucket(gcsBucket);
用你的projectId和keyFilename。