【问题标题】:Firebase schedule data exports PERMISSION_DENIED: The caller does not have permissionFirebase计划数据导出PERMISSION_DENIED:调用者没有权限
【发布时间】: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',
      });
    }
  });

我在这里错过了什么?

【问题讨论】:

标签: firebase google-cloud-firestore google-cloud-storage


【解决方案1】:

您遇到的错误是常见错误,可能有多种原因-

  1. 如 Juan Lara 所述,项目 ID 中可能没有 根据this document 的环境,如果您使用的是 运行时比 Node JS 8 更新。
  2. 云功能使用的服务账号没有 必要的许可。但是你提到你已经给了 使用命令的权限,所以这似乎不是这里的问题。 我仍然建议您在 IAM 部分中重新验证它们 谷歌云控制台。
  3. 如果您已将 Firestore 数据库初始化为锁定模式,则 默认情况下,所有读写操作都被阻止。

规则如下:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

我建议您将其更改为以下内容并尝试:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth !=null;
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    相关资源
    最近更新 更多