【发布时间】:2021-08-13 15:05:58
【问题描述】:
我正在尝试使用this official guide 中记录的云功能设置我的 Firestore 数据库的计划备份,但每次运行该功能时,都会出现错误:
PERMISSION_DENIED: The caller does not have permission
如指南中所述,我通过运行以下命令为我的默认服务帐户赋予了“Admin Data Import Export”角色:
gcloud projects add-iam-policy-binding wag-prod \
--member serviceAccount:wag-prod@appspot.gserviceaccount.com \
--role roles/datastore.importExportAdmin
然后通过运行以下命令在存储桶权限中将“Storage Admin”角色添加到此帐户:
gsutil iam ch serviceAccount:wag-prod@appspot.gserviceaccount.com:admin \
gs://wag-prod-db-backup
这是我的云功能:
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
// contants
import { CF_LOCATION } from '../../constants/cf_config';
import { LoggingHelper } from '../../helpers/logging_helper';
const bucket = 'gs://wag-prod-db-backup';
const client = new admin.firestore.v1.FirestoreAdminClient({});
export default functions
.region(CF_LOCATION)
.pubsub.schedule('every day 00:00')
.onRun(async () => {
const projectId = process.env.GCP_PROJECT;
try {
const res = await client.exportDocuments({
name: `projects/${projectId}/databases/(default)`,
outputUriPrefix: bucket,
// Leave collectionIds empty to export all collections
// or set to a list of collection IDs to export,
// collectionIds: ['users', 'posts']
collectionIds: [],
});
LoggingHelper.logFunction(
'scheduled:databaseBackup',
{
caller_id: 'none',
},
res,
);
} catch (error) {
LoggingHelper.logFunctionError('internal', error.message, {
caller_id: 'none',
});
}
});
我在这里错过了什么?
【问题讨论】:
-
projectId的值是否正确?看起来process.env.GCP_PROJECT是not automatically available in new runtimes。
标签: firebase google-cloud-firestore google-cloud-storage