随着Firebase云功能的引入,我们正在考虑将一些当前的node.js服务器端代码移动到云功能。我遇到的一个问题是将文件从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工具。我认为&#34;它是GCS的身份验证错误,但我不确定如何跟踪它。我是否需要在GCS的云函数.config()中使用与Firebase不同的auth参数?如果是这样,我该如何输入?我们的GCS存储桶和项目早于Firebase存储的引入,但我们已成功将其与我们服务器上运行的节点功能一起使用。
提前致谢, 扎克
答案 0 :(得分:3)
_(ツ)_ /¯
在我们的项目设置中可能是一种奇怪的行为,但认为与更新解决问题的方法相关。
使用上面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);
答案 1 :(得分:3)
在不附加projectId或keyFilename的情况下解决此问题的另一种方法:
答案 2 :(得分:0)
您需要先识别才能使用Google云存储API。或顺便说一句任何其他API。
一种方法是转到Google云平台上的API登录页面:here并将相应的服务帐户密钥下载为JSON文件。然后将此文件放在firebase项目的functions文件夹中。
最后不要使用:
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。