使用 firebase 函数从 firebase 存储读取并写入 firestore
Read from firebase storage and write to firestore using firebase functions
我试过这个打字稿代码
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import serviceAccount from "/Users/300041370/Downloads/serviceKey.json";
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
const buckObj = functions.storage.bucket("myBucket").object();
export const onWikiWrite = buckObj.onFinalize(async (object) => {
const filePath = object.name ?? "test.json";
const bucket = admin.storage().bucket("myBucket");
bucket.file(filePath).download().then((data) => {
const contents = data[0];
data = {"key": "value"};
const doc = admin.firestore().collection("myCollection").doc();
doc.set(data);
});
});
但这给了我以下错误
"status":{"code":7,"message":"Insufficient permissions to (re)configure a trigger (permission denied for bucket myBucket). Please, give owner permissions to the editor role of the bucket and try again.
我问过这个question here but it got closed as duplicate of 。它基本上说,不支持 storage.bucket("myBucket")
功能,我将不得不使用 match
来限制此特定 bucket/folder 中的文件的操作。因此,我尝试了这个
const buckObj = functions.storage.object();
export const onWikiWrite = buckObj.onFinalize(async (object) => {
if (object.name.match(/myBucket\//)) {
const fileBucket = object.bucket;
const filePath = object.name;
const bucket = admin.storage().bucket(fileBucket);
bucket.file(filePath).download().then((data) => {
const contents = data[0];
const doc = admin.firestore().collection("myCollection").doc();
const data = {content: contents}
doc.set(data);
});
}
});
我仍然面临同样的问题。我会在这里重复一遍:
"status":{"code":7,"message":"Insufficient permissions to (re)configure a trigger (permission denied for bucket myBucket). Please, give owner permissions to the editor role of the bucket and try again.
自 Firebase SDK for Cloud Functions 版本 1.0 起,firebase-admin
在 Cloud Functions 运行时中应为 initialized without any parameters。
以下应该有效(我已经删除了 filePath
上的检查):
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
export const onWikiWrite = functions.storage
.object()
.onFinalize(async (object) => {
const fileBucket = object.bucket;
const filePath = object.name;
const bucket = admin.storage().bucket(fileBucket);
return bucket
.file(filePath)
.download()
.then((data) => {
const contents = data[0];
return admin
.firestore()
.collection('myCollection')
.add({ content: contents });
});
});
请注意,我们 return chain of promises return 由异步 Firebase 方法编辑。在执行异步处理(也称为“后台函数”)的 Cloud Functions 中,关键是 return JavaScript promise 当所有异步处理完成时。
我们也使用add()
方法而不是doc().set()
。
最后,在检查filePath
的值时,请注意云存储中实际上没有文件夹或子目录的概念(参见)。
我试过这个打字稿代码
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import serviceAccount from "/Users/300041370/Downloads/serviceKey.json";
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
const buckObj = functions.storage.bucket("myBucket").object();
export const onWikiWrite = buckObj.onFinalize(async (object) => {
const filePath = object.name ?? "test.json";
const bucket = admin.storage().bucket("myBucket");
bucket.file(filePath).download().then((data) => {
const contents = data[0];
data = {"key": "value"};
const doc = admin.firestore().collection("myCollection").doc();
doc.set(data);
});
});
但这给了我以下错误
"status":{"code":7,"message":"Insufficient permissions to (re)configure a trigger (permission denied for bucket myBucket). Please, give owner permissions to the editor role of the bucket and try again.
我问过这个question here but it got closed as duplicate of storage.bucket("myBucket")
功能,我将不得不使用 match
来限制此特定 bucket/folder 中的文件的操作。因此,我尝试了这个
const buckObj = functions.storage.object();
export const onWikiWrite = buckObj.onFinalize(async (object) => {
if (object.name.match(/myBucket\//)) {
const fileBucket = object.bucket;
const filePath = object.name;
const bucket = admin.storage().bucket(fileBucket);
bucket.file(filePath).download().then((data) => {
const contents = data[0];
const doc = admin.firestore().collection("myCollection").doc();
const data = {content: contents}
doc.set(data);
});
}
});
我仍然面临同样的问题。我会在这里重复一遍:
"status":{"code":7,"message":"Insufficient permissions to (re)configure a trigger (permission denied for bucket myBucket). Please, give owner permissions to the editor role of the bucket and try again.
自 Firebase SDK for Cloud Functions 版本 1.0 起,firebase-admin
在 Cloud Functions 运行时中应为 initialized without any parameters。
以下应该有效(我已经删除了 filePath
上的检查):
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
export const onWikiWrite = functions.storage
.object()
.onFinalize(async (object) => {
const fileBucket = object.bucket;
const filePath = object.name;
const bucket = admin.storage().bucket(fileBucket);
return bucket
.file(filePath)
.download()
.then((data) => {
const contents = data[0];
return admin
.firestore()
.collection('myCollection')
.add({ content: contents });
});
});
请注意,我们 return chain of promises return 由异步 Firebase 方法编辑。在执行异步处理(也称为“后台函数”)的 Cloud Functions 中,关键是 return JavaScript promise 当所有异步处理完成时。
我们也使用add()
方法而不是doc().set()
。
最后,在检查filePath
的值时,请注意云存储中实际上没有文件夹或子目录的概念(参见