无法开始备份:请求失败,状态码为400 Google Cloud [英] Could not start backup:Request failed with status code 400 Google Cloud

查看:112
本文介绍了无法开始备份:请求失败,状态码为400 Google Cloud的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Firestore创建一个备份系统.当我尝试部署该指南的每一步时,代码,它返回了请求失败,状态码为400

I'm trying to create a backup system for Firestore. I followed every step of this guide and when I tried to deploy the code, it returned Request failed with status code 400

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

PROJECT-ID@appspot.gserviceaccount.com permissions: Cloud Datastore Import Export Admin, Editor, Storage Admin

这是app.js的代码

This is the code of 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.');
});

我还有另一个正在监听"/"的功能.可能会引起问题吗?

I have another function that is listening to '/'. Is possible that this can cause the problem?

package.json:

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错误

I also look inside Cron log and there is nothing related to the error. It only returns 500 error

推荐答案

主要问题是铲斗位置

The main problem is the bucket location

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

When you create a backup bucket you must use Multi-Region or you will receive a deny from the server.

我认为这是Google Cloud的错误

I think this is a bug of Google Cloud

解决方案

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

Delete the bucket and create a new one with a Multi-Regional location

单一位置的错误:

'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.',

这篇关于无法开始备份:请求失败,状态码为400 Google Cloud的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆