【问题标题】:How to schedule a Cloud Datastore export with App Engine in Python3如何在 Python3 中使用 App Engine 安排 Cloud Datastore 导出
【发布时间】:2019-10-15 15:31:54
【问题描述】:

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

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

根据docs,app使用app_identity库获取token。

    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?

【问题讨论】:

  • 你确定不支持吗?对我来说,这只是他们的文档不是最新的。 app_identity 应该在 python3 中有对应的
  • 导入 google.appengine 在 python3 中出现错误:ModuleNotFoundError: No module named 'google.appengine'

标签: python python-3.x google-app-engine oauth-2.0 google-cloud-datastore


【解决方案1】:

查看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 部署到非默认服务)

requirements.txt

Flask
google-api-python-client

ma​​in.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)

【讨论】:

    猜你喜欢
    • 2019-06-19
    • 2014-04-10
    • 1970-01-01
    • 2015-09-06
    • 2017-04-13
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多