【发布时间】:2018-12-24 08:10:45
【问题描述】:
为了确保它尽可能干净和隔离,我创建了一个新的测试项目,除了这些步骤之外什么都没有。
然后我在控制台中启用了 Cloud Firestore。我创建了一个名为“blarg”的新系列(好名字,嗯!)。我向其中添加了一个文档,并使用了自动 ID,然后添加了一个名为“humpty”的字段和字符串 dumpty。在控制台中是这样的:
我在 Firebase 控制台的存储部分创建了一个名为 signatures 的目录。当我的函数(如下)触发时,我想在这个目录中写入一个文件。
然后我使用以下代码添加了一个云功能。它在函数部分正确显示(我假设),名称为testItOut,并在/blarg/{eventId} 的事件document.update 上触发。:
const functions = require('firebase-functions');
const os = require('os');
const fs = require('fs');
const path = require('path');
require('firebase-admin').initializeApp();
exports.testItOut = functions.firestore
.document('blarg/{docId}')
.onUpdate((change, context) => {
console.log( "Inside #testItOut" );
const projectId = 'testing-60477';
const Storage = require('@google-cloud/storage');
const storage = new Storage({
projectId,
});
let bucketName = 'signatures';
let fileName = 'temp.txt';
const tempFilePath = path.join(os.tmpdir(), fileName);
console.log( `Writing out to ${tempFilePath}` );
fs.writeFileSync(tempFilePath, "something!" );
return storage
.bucket( bucketName )
.upload( tempFilePath )
.then( () => fs.unlinkSync(tempFilePath) )
.catch(err => console.error('ERROR inside upload: ', err) );
});
package.json 看起来像这样:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"@google-cloud/storage": "^1.7.0",
"firebase-admin": "~5.12.1",
"firebase-functions": "^1.0.3"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}
如果我更改键“humpty”的值,我会看到函数调用。但是,我在日志中得到了错误。
ERROR inside upload: { ApiError: testing-60477@appspot.gserviceaccount.com does not have storage.objects.create access to signatures/temp.txt.
at Object.parseHttpRespBody (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:193:30)
at Object.handleResp (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:131:18)
...
我认为这很简单。我究竟做错了什么?我以为调用initializeApp() 会授予我的函数权限以自动写入帐户的存储桶?
我看到的唯一奇怪的错误是未设置结算。我认为只有在您使用外部 API 时才有必要这样做。 Google Storage 是“外部 API”吗?日志消息并未表明这是问题所在。
【问题讨论】:
标签: firebase google-cloud-storage google-cloud-firestore google-cloud-functions