【问题标题】:Could not start backup:Request failed with status code 400 Google Cloud无法开始备份:请求失败,状态码为 400 Google Cloud
【发布时间】:2019-09-05 10:13:00
【问题描述】:

我正在尝试为 Firestore 创建备份系统。 我遵循了guide 的每一步,当我尝试部署代码时,它返回了Request failed with status code 400

PROJECT-ID@appspot.gserviceaccount.com 权限:云数据存储区导入导出管理员, 编辑, 存储管理员

这是app.js的代码

'use strict';


const axios = require('axios');
const dateformat = require('dateformat');
const { google } = require('googleapis');
const express = require('express');
const util = require('util')
const request = require('request');
const admin = require('firebase-admin');

const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();


admin.initializeApp({
  credential: admin.credential.applicationDefault()
});

const db = admin.firestore();

const googleMapsClient = require('@google/maps').createClient({
  key: 'AIza*****',
  Promise: Promise
});

const app = express();

// Trigger a backup
app.get('/cloud-firestore-export', async (req, res) => {
  const auth = await google.auth.getClient({
    scopes: ['https://www.googleapis.com/auth/datastore'],
  });

  const accessTokenResponse = await auth.getAccessToken();
  const accessToken = accessTokenResponse.token;

  const headers = {
    'Content-Type': 'application/json',
    Authorization: 'Bearer ' + accessToken,
  };

  const { outputUriPrefix } = req.query;
  if (!outputUriPrefix) {
    res.status(500).send('outputUriPrefix required');
  } else if (outputUriPrefix && outputUriPrefix.indexOf('gs://') !== 0) {
    res.status(500).send('Malformed outputUriPrefix: ${outputUriPrefix}');
  }

  // Construct a backup path folder based on the timestamp
  const timestamp = dateformat(Date.now(), 'yyyy-mm-dd-HH-MM-ss');
  let path = outputUriPrefix;
  if (path.endsWith('/')) {
    path += timestamp;
  } else {
    path += '/' + timestamp;
  }

  const body = {
    outputUriPrefix: path,
  };

  // If specified, mark specific collections for backup
  const { collections } = req.query;
  if (collections) {
    body.collectionIds = collections.split(',');
  }

  const projectId = process.env.GOOGLE_CLOUD_PROJECT;
  const url = 'https://firestore.googleapis.com/v1beta1/projects/' + projectId + '/databases/(default):exportDocuments';

  try {
    const response = await axios.post(url, body, { headers });
    res
      .status(200)
      .send(response.data)
      .end();
  } catch (e) {
    if (e.response) {
      console.warn(e.response.data);
    }

    res
      .status(500)
      .send('Could not start backup:' + e.message)
      .end();
  }
});


°°°°
// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log('App listening on port ${PORT}');
  console.log('Press Ctrl+C to quit.');
});

我有另一个正在收听“/”的函数。这可能会导致问题吗?

package.json:

{
  "name": "solution-scheduled-backups",
   "version": "1.0.0",
  "description": "Scheduled Cloud Firestore backups via AppEngine cron",
  "main": "app.js",
  "engines": {
    "node": "10.x.x"
  },
  "scripts": {
    "deploy": "gcloud app deploy --quiet app.yaml cron.yaml",
    "start": "node app.js"
  },
  "author": "Google, Inc.",
  "license": "Apache-2.0",
  "dependencies": {
    "@google-cloud/storage": "^3.2.1",
    "@google/maps": "^0.5.5",
    "axios": "^0.19.0",
    "dateformat": "^3.0.3",
    "express": "^4.17.1",
    "firebase-admin": "^8.4.0",
    "googleapis": "^42.0.0",
    "request": "^2.88.0"
  },
  "devDependencies": {
    "prettier": "^1.18.2"
  }
}

我还查看了 Cron 日志,没有任何与错误相关的内容。它只返回500 error

【问题讨论】:

  • 您好 Matteo,我正在查看您的代码与您所遵循的指南之间的差异,目前我看到您没有在应用程序顶部定义常量。 js 就像他们在指南中所做的那样,我也看到你实际上并没有启动服务器。您的 app.js 中是否确实有这些详细信息,或者您是否忘记根据需要添加/删除它们?
  • @dhauptman 是的,这是因为我还有另一个功能。我更新了代码请检查

标签: google-app-engine google-cloud-platform google-cloud-storage google-iam


【解决方案1】:

主要问题是桶位置

当您创建备份存储桶时,您必须使用Multi-Region,否则您将收到来自服务器的拒绝。

我认为这是 Google Cloud 的错误

解决方案

删除存储桶并创建一个具有多区域位置的新存储桶

单一位置的错误:

'Bucket backup-bucket is in location EUR4. This project can only operate on buckets 
spanning location europe-north1 or europe-west1 or eu or europe-west2 or 
europe-west3 or europe-west4 or europe-west5 or europe-west6.',

【讨论】:

    【解决方案2】:

    所以我一直在复制您的问题并找到了解决方案。

    我一直遇到和你一样的错误,我终于设法解决了。如果您查看 GAE 日志,您会看到错误提示“Project \' "YOUR PROJECT" \' 不是启用 Cloud Firestore 的项目。'。

    这对我有用:

    1. 创建一个新项目。
    2. 转到 GCP 中的 API 库并启用 Firestore 安装 API。
    3. 转到Firebase 并将您的 GCP 项目链接到一个 firebase 项目。
    4. 转到数据库并创建 Firestore 数据库。
    5. 按照具有权限和部署到应用引擎的存储库进行操作。
    6. 测试 cron 会成功。

    如果您曾在实际项目中启用过 DataStore,您将无法在第 3 步创建 Firestore 实例。

    这将为您创建所需的 .appspot.com 格式的存储桶,您有权访问这些存储桶。

    转到 GAE 并创建您的 cron.yaml、app.js 以及您需要的所有其他内容。我使用this repo 进行测试。

    在 repo 的 readme.md 中,您拥有为授予服务帐户权限而必须执行的确切命令。

    记得按照 cron.yaml 中的说明更改存储桶。

    按照 repo 中提到的步骤操作,因为它们做得很好。

    如果它对你有用,请告诉我!

    【讨论】:

      猜你喜欢
      • 2017-06-02
      • 2019-09-29
      • 2020-10-21
      • 2021-11-09
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 2021-01-12
      相关资源
      最近更新 更多