#前提
↓のチュートリアルをこなしていました。GAE、python3, cloud datastore を使っている公式チュートリアルです。
https://cloud.google.com/appengine/docs/standard/python3/building-app/storing-and-retrieving-data
#解決したい疑問
ローカルで動かしたら正常に動作するにもかかわらず、GAEにデプロイ後のサービスが正常に動作せず困っております。
チュートリアルの内容を簡単に言うと
・サイトにきたユーザーのアクセス時間を記録し、cloud datastoreに貯めていく(ユーザー識別しない状態)
・貯めたdatastoreから、最新のアクセス時間10個を画面のhtmlに埋め込み、表示させる
です。
ローカルで動かしたとき(python3 main.py)は予想した通りに正常に動きました。
しかし、GAEにデプロイしたとき、予想した通りに動きませんでした。
デプロイはエラー無く出来たのですが、GAEのappにアクセスしてもcloud datastore にアクセス時間を保存できていません。
(GCPのコンソール→cloud datastoreのエンティティから確認済み)
そのため、ページの表示も更新されない状況となっています。
なぜ、cloud datastore がデプロイ後だと、保存できないのでしょうか。
何か情報お持ちの方、アドバイス頂けませんでしょうか。
*ローカルで動かしたとき(ページリロードにそって、正しく動作している)
*デプロイしたサービスを動かしたとき(ページリロードしているのに正しく動作できていない)
以下にチュートリアルのコードをそのまま記載いたします。
・app.yaml
yaml
1runtime: python37 2 3handlers: 4 # This configures Google App Engine to serve the files in the app's static 5 # directory. 6- url: /static 7 static_dir: static 8 9 # This handler routes all requests not caught above to your main app. It is 10 # required when static routes are defined, but can be omitted (along with 11 # the entire handlers section) when there are no static files defined. 12- url: /.* 13 script: auto
・main.py
python3
1# Copyright 2018 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import datetime 16 17from flask import Flask, render_template 18 19# [START gae_python37_datastore_store_and_fetch_times] 20from google.cloud import datastore 21 22datastore_client = datastore.Client() 23 24# [END gae_python37_datastore_store_and_fetch_times] 25app = Flask(__name__) 26 27 28# [START gae_python37_datastore_store_and_fetch_times] 29def store_time(dt): 30 entity = datastore.Entity(key=datastore_client.key('visit')) 31 entity.update({ 32 'timestamp': dt 33 }) 34 35 datastore_client.put(entity) 36 37 38def fetch_times(limit): 39 query = datastore_client.query(kind='visit') 40 query.order = ['-timestamp'] 41 42 times = query.fetch(limit=limit) 43 44 return times 45# [END gae_python37_datastore_store_and_fetch_times] 46 47 48# [START gae_python37_datastore_render_times] 49@app.route('/') 50def root(): 51 # Store the current access time in Datastore. 52 store_time(datetime.datetime.now()) 53 54 # Fetch the most recent 10 access times from Datastore. 55 times = fetch_times(10) 56 print("this is %s" % type(times)) 57 58 return render_template( 59 'index.html', times=times) 60# [END gae_python37_datastore_render_times] 61 62 63if __name__ == '__main__': 64 # This is used when running locally only. When deploying to Google App 65 # Engine, a webserver process such as Gunicorn will serve the app. This 66 # can be configured by adding an `entrypoint` to app.yaml. 67 68 # Flask's development server will automatically serve static files in 69 # the "static" directory. See: 70 # http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed, 71 # App Engine itself will serve those files as configured in app.yaml. 72 app.run(host='127.0.0.1', port=8080, debug=True)
・requirement.txt
txt
1Flask==1.0.2 2google-cloud-datastore==1.7.3
・index.html
html
1<!doctype html> 2<html> 3<head> 4 <title>Datastore and Firebase Auth Example</title> 5 <script src="{{ url_for('static', filename='script.js') }}"></script> 6 <link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> 7</head> 8<body> 9 10 <h1>Datastore and Firebase Auth Example</h1> 11 <!-- [START gae_python37_datastore_print_times] --> 12 <h2>Last 10 visits</h2> 13 {% for time in times %} 14 <p>{{ time['timestamp'] }}</p> 15 {% endfor %} 16 <!-- [END gae_python37_datastore_print_times] --> 17</body> 18</html>
回答1件
あなたの回答
tips
プレビュー