如何在 Python3 中使用 App Engine 安排 Cloud Datastore 导出

How to schedule a Cloud Datastore export with App Engine in Python3

我有一个将用户数据存储在 GCP 数据存储中的应用程序。 我做了一个 cron 作业,计划使用给定的说明导出 Datastore 中的数据 here

但是,我需要将 python2 更改为 python3。

根据 docs,应用程序使用 app_identity 库来获取令牌。

    from google.appengine.api import app_identity
    access_token, _ = app_identity.get_access_token('https://www.googleapis.com/auth/datastore')

但根据 here,python3 不支持此库。

如何在 python3 中获得 access_token?

检查 google-api-python-client 图书馆。它在 python 3 中受支持,可以轻松地为 Cloud Datastore API.

构建请求

您需要更改的另一件事是 webapp2 库,因为 python 也不支持它 3. 您可以用 Flask 之类的东西替换它。

这是为 python 3 重写的应用示例:

app.yaml

runtime: python37

handlers:
- url: /.*
  script: auto

(如果需要,使用 service: service_name 部署到 non-default 服务)

requirements.txt

Flask
google-api-python-client

main.py

import datetime
import os
from googleapiclient.discovery import build

from flask import Flask, render_template, request

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello, World!'    

@app.route('/cloud-datastore-export')
def export():
    # Deny if not from the Cron Service
    assert request.headers['X-Appengine-Cron']
    # Deny if output_url_prefix not set correctly
    output_url_prefix = request.args.get('output_url_prefix')
    assert output_url_prefix and output_url_prefix.startswith('gs://')
    timestamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
    if '/' not in output_url_prefix[5:]:
      # Only a bucket name has been provided - no prefix or trailing slash
      output_url_prefix += '/' + timestamp
    else:
      output_url_prefix += timestamp
    kinds = request.args.getlist('kind')
    namespace_ids = request.args.getlist('namespace_id')
    entity_filter = {
        'kinds': kinds,
        'namespace_ids': namespace_ids 
    }
    body = {
        'output_url_prefix': output_url_prefix,
        'entity_filter': entity_filter
    }
    project_id = os.environ.get('GOOGLE_CLOUD_PROJECT')
    client = build('datastore', 'v1')
    client.projects().export(projectId=project_id, body=body).execute()  
    return 'Operation started' 


if __name__ == '__main__':
    # This is used when running locally only. When deploying to Google App
    # Engine, a webserver process such as Gunicorn will serve the app. This
    # can be configured by adding an `entrypoint` to app.yaml.
    # Flask's development server will automatically serve static files in
    # the "static" directory. See:
    # http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed,
    # App Engine itself will serve those files as configured in app.yaml.
    app.run(host='127.0.0.1', port=8080, debug=True)