GAE 数据存储权限被拒绝?
GAE Datastore Permission Denied?
我正在尝试学习如何使用 Google App Engine,并且我正在使用 Python here 的快速入门文档。这是一个简单的代码,用于将时间戳写入数据存储,然后将它们打印回给用户。
我相当严格地遵循代码和步骤,但我使用的是云 Shell 和云控制台,而不是在本地下载 SDK 和 运行 代码。
代码的前半部分运行良好,我可以部署 Web 服务。但是,当我开始使用数据存储时,我的应用程序中出现 500 Internal Server Error
。下面提供了完整的 main.py
。我注释掉了部分代码,发现问题似乎是在写入数据存储区时出现的。错误日志显示 PERMISSION DENIED
错误(下面的日志)。
据我所知,使用云 Shell 应该意味着我的 python 脚本和数据存储之间的身份验证应该 "just work",但看起来它没有.我还使用了 Google 文档中的确切代码,所以我不明白为什么会出现错误。
有什么想法可以尝试吗?
这是我的 main.py
:
import datetime
from flask import Flask, render_template
from google.cloud import datastore
datastore_client = datastore.Client()
app = Flask(__name__)
def store_time(dt):
entity = datastore.Entity(key=datastore_client.key('visit'))
entity.update({
'timestamp': dt
})
datastore_client.put(entity)
def fetch_times(limit):
query = datastore_client.query(kind='visit')
query.order = ['-timestamp']
times = query.fetch(limit=limit)
return times
@app.route('/')
def root():
store_time(datetime.datetime.now())
times = fetch_times(10)
return render_template(
'index.html', times=times)
错误日志显示以下错误:
File "", line 3, in raise_from:
google.api_core.exceptions.PermissionDenied: 403 Missing or
insufficient permissions. at error_remapped_callable
(/env/lib/python3.7/site-packages/google/api_core/grpc_helpers.py:59)
at func_with_timeout
(/env/lib/python3.7/site-packages/google/api_core/timeout.py:214) at
retry_target
(/env/lib/python3.7/site-packages/google/api_core/retry.py:184) at
retry_wrapped_func
(/env/lib/python3.7/site-packages/google/api_core/retry.py:286) at
call (/env/lib/python3.7/site-packages/google/api_core/gapic_v1/method.py:143)
at commit
(/env/lib/python3.7/site-packages/google/cloud/datastore_v1/gapic/datastore_client.py:571)
at _commit
(/env/lib/python3.7/site-packages/google/cloud/datastore/batch.py:250)
at commit
(/env/lib/python3.7/site-packages/google/cloud/datastore/batch.py:274)
at put_multi
(/env/lib/python3.7/site-packages/google/cloud/datastore/client.py:490)
at put
(/env/lib/python3.7/site-packages/google/cloud/datastore/client.py:463)
at store_time (/srv/main.py:20) at root (/srv/main.py:36) at
dispatch_request (/env/lib/python3.7/site-packages/flask/app.py:1935)
at full_dispatch_request
(/env/lib/python3.7/site-packages/flask/app.py:1949) at reraise
(/env/lib/python3.7/site-packages/flask/_compat.py:39) at
handle_user_exception
(/env/lib/python3.7/site-packages/flask/app.py:1820) at
full_dispatch_request
(/env/lib/python3.7/site-packages/flask/app.py:1951) at wsgi_app
(/env/lib/python3.7/site-packages/flask/app.py:2446)
我使用了 Cloud Shell 并按原样使用了您的代码。你的代码对我有用。
创建时间:
PROJECT=[[YOUR-PROJECT]]
REGION=[[YOUR-REGION]] # us-west2
gcloud app create --project=${PROJECT} --region=${REGION}
有:
app.yaml
:
runtime: python37
entrypoint: gunicorn -b :$PORT main:app
NB entrypoint
is optional but I prefer to be explicit
requirements.txt
:
Flask==1.1.2
gunicorn==20.0.4
google-cloud-datastore==1.12.0
并使用了示例模板:
templates/index.html
:
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h2>Visits:</h2>
{% for time in times %}
<p>{{ time }}</p>
{% endfor %}
</body>
</html>
并且:
gcloud app deploy app.yaml --project=${PROJECT}
然后浏览应用端点:
Visits:
<Entity('visit', 5079418695319552) {'timestamp': datetime.datetime(2020, 4, 12, 20, 20, 29, 417656, tzinfo=<UTC>)}>
<Entity('visit', 5702893864747008) {'timestamp': datetime.datetime(2020, 4, 12, 20, 16, 28, 305081, tzinfo=<UTC>)}>
<Entity('visit', 5636645067948032) {'timestamp': datetime.datetime(2020, 4, 12, 20, 16, 26, 495333, tzinfo=<UTC>)}>
<Entity('visit', 5642368648740864) {'timestamp': datetime.datetime(2020, 4, 12, 20, 16, 22, 369022, tzinfo=<UTC>)}>
<Entity('visit', 5632499082330112) {'timestamp': datetime.datetime(2020, 4, 12, 20, 16, 20, 659993, tzinfo=<UTC>)}>
我正在尝试学习如何使用 Google App Engine,并且我正在使用 Python here 的快速入门文档。这是一个简单的代码,用于将时间戳写入数据存储,然后将它们打印回给用户。
我相当严格地遵循代码和步骤,但我使用的是云 Shell 和云控制台,而不是在本地下载 SDK 和 运行 代码。
代码的前半部分运行良好,我可以部署 Web 服务。但是,当我开始使用数据存储时,我的应用程序中出现 500 Internal Server Error
。下面提供了完整的 main.py
。我注释掉了部分代码,发现问题似乎是在写入数据存储区时出现的。错误日志显示 PERMISSION DENIED
错误(下面的日志)。
据我所知,使用云 Shell 应该意味着我的 python 脚本和数据存储之间的身份验证应该 "just work",但看起来它没有.我还使用了 Google 文档中的确切代码,所以我不明白为什么会出现错误。
有什么想法可以尝试吗?
这是我的 main.py
:
import datetime
from flask import Flask, render_template
from google.cloud import datastore
datastore_client = datastore.Client()
app = Flask(__name__)
def store_time(dt):
entity = datastore.Entity(key=datastore_client.key('visit'))
entity.update({
'timestamp': dt
})
datastore_client.put(entity)
def fetch_times(limit):
query = datastore_client.query(kind='visit')
query.order = ['-timestamp']
times = query.fetch(limit=limit)
return times
@app.route('/')
def root():
store_time(datetime.datetime.now())
times = fetch_times(10)
return render_template(
'index.html', times=times)
错误日志显示以下错误:
File "", line 3, in raise_from: google.api_core.exceptions.PermissionDenied: 403 Missing or insufficient permissions. at error_remapped_callable (/env/lib/python3.7/site-packages/google/api_core/grpc_helpers.py:59) at func_with_timeout (/env/lib/python3.7/site-packages/google/api_core/timeout.py:214) at retry_target (/env/lib/python3.7/site-packages/google/api_core/retry.py:184) at retry_wrapped_func (/env/lib/python3.7/site-packages/google/api_core/retry.py:286) at call (/env/lib/python3.7/site-packages/google/api_core/gapic_v1/method.py:143) at commit (/env/lib/python3.7/site-packages/google/cloud/datastore_v1/gapic/datastore_client.py:571) at _commit (/env/lib/python3.7/site-packages/google/cloud/datastore/batch.py:250) at commit (/env/lib/python3.7/site-packages/google/cloud/datastore/batch.py:274) at put_multi (/env/lib/python3.7/site-packages/google/cloud/datastore/client.py:490) at put (/env/lib/python3.7/site-packages/google/cloud/datastore/client.py:463) at store_time (/srv/main.py:20) at root (/srv/main.py:36) at dispatch_request (/env/lib/python3.7/site-packages/flask/app.py:1935) at full_dispatch_request (/env/lib/python3.7/site-packages/flask/app.py:1949) at reraise (/env/lib/python3.7/site-packages/flask/_compat.py:39) at handle_user_exception (/env/lib/python3.7/site-packages/flask/app.py:1820) at full_dispatch_request (/env/lib/python3.7/site-packages/flask/app.py:1951) at wsgi_app (/env/lib/python3.7/site-packages/flask/app.py:2446)
我使用了 Cloud Shell 并按原样使用了您的代码。你的代码对我有用。
创建时间:
PROJECT=[[YOUR-PROJECT]]
REGION=[[YOUR-REGION]] # us-west2
gcloud app create --project=${PROJECT} --region=${REGION}
有:
app.yaml
:
runtime: python37
entrypoint: gunicorn -b :$PORT main:app
NB
entrypoint
is optional but I prefer to be explicit
requirements.txt
:
Flask==1.1.2
gunicorn==20.0.4
google-cloud-datastore==1.12.0
并使用了示例模板:
templates/index.html
:
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h2>Visits:</h2>
{% for time in times %}
<p>{{ time }}</p>
{% endfor %}
</body>
</html>
并且:
gcloud app deploy app.yaml --project=${PROJECT}
然后浏览应用端点:
Visits:
<Entity('visit', 5079418695319552) {'timestamp': datetime.datetime(2020, 4, 12, 20, 20, 29, 417656, tzinfo=<UTC>)}>
<Entity('visit', 5702893864747008) {'timestamp': datetime.datetime(2020, 4, 12, 20, 16, 28, 305081, tzinfo=<UTC>)}>
<Entity('visit', 5636645067948032) {'timestamp': datetime.datetime(2020, 4, 12, 20, 16, 26, 495333, tzinfo=<UTC>)}>
<Entity('visit', 5642368648740864) {'timestamp': datetime.datetime(2020, 4, 12, 20, 16, 22, 369022, tzinfo=<UTC>)}>
<Entity('visit', 5632499082330112) {'timestamp': datetime.datetime(2020, 4, 12, 20, 16, 20, 659993, tzinfo=<UTC>)}>