无法开始备份:请求失败,状态码为400 Google Cloud

时间:2019-09-05 10:13:00

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

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

PROJECT-ID@appspot.gserviceaccount.com权限:Cloud Datastore导入导出管理员, 编辑, 存储管理员

这是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

2 个答案:

答案 0 :(得分:0)

因此,我一直在复制您的问题,并找到了解决方案。

我一直遇到与您相同的错误,最终我设法弄清楚了。如果查看GAE日志,则会看到错误消息,指出“项目\”“您的项目” \'不是已启用Cloud Firestore的项目。”。

这对我有用:

  1. 创建一个新项目。
  2. 转到GCP中的API库,然后启用Firestore安装API。
  3. 转到Firebase并将您的GCP项目链接到firebase项目。
  4. 转到数据库并创建Firestore数据库。
  5. 在回购之后添加权限和对App Engine的部署。
  6. 测试cron,它将成功。

如果您在实际项目中启用了DataStore,则在第3步将无法执行Firestore实例。

这将为您创建您拥有权限的.appspot.com格式的所需存储桶。

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

在存储库的readme.md中,您具有为授予服务帐户权限所必须执行的确切命令。

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

按照回购协议中提到的步骤进行,因为它们已经做得很好。

让我知道它是否对您有用!

答案 1 :(得分:0)

  

主要问题是铲斗位置

创建备份存储桶时,您必须使用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.',
相关问题