2017-03-27 129 views
2

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

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

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认证错误,但我不知道如何跟踪。对于GCS,我需要在云功能的.config()中使用不同的身份验证参数吗?如果是这样,我怎么输入它们?我们的GCS存储桶和项目在Firebase存储的引入之前发布,但我们已成功将它用于运行在我们服务器上的节点功能。

由于提前, 扎克

回答

0

你需要能够使用谷歌的云存储API之前识别。或者其他任何API。

这样做的一种方法是转到Google云平台上的API登录页面:here并将相应的服务帐户密钥下载为JSON文件。然后将这个文件放在您的Firebase Project内的函数文件夹中。

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

你会称之为:

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

const storageClient = gcs({ projectId: projectId, keyFilename: './keyFilename.json' }); const bucket = storageClient.bucket(gcsBucket);
有了您的专案编号和的KeyFileName。

+0

感谢您的回复。不幸的是,我已经尝试过了,但它不起作用。我想我可能有一个认证问题,但是在追踪时遇到问题。 – Zach

3

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

_(ツ)_ /¯

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

使用grll上面提供的格式是:工作的配置是:

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); 
2

另一种方式来解决这个不附加专案编号或的KeyFileName:

  1. 转到项目的火力地堡控制台
  2. 设置 - >权限
  3. 应打开IAM选项卡
  4. 现在为'存储管理员'角色提供您的'App Engine默认服务帐户'和'Google API服务帐户'权限
  5. 现在应该没问题!
+0

找不到存储管理员角色。我应该在该角色上创建一个新的基础,然后将其分配给该帐户?此外,“Google API服务帐户”不再存在。 – Xerri

相关问题