GAE NDB Datastore new feature: Access Datastore entities from other GAE app

对着背影说爱祢 提交于 2019-12-20 11:01:10

问题


Reading the new documentation of GAE NDB Datastore: https://cloud.google.com/appengine/docs/python/ndb/modelclass#class_methods

get_by_id(id, parent=None, app=None, namespace=None, **ctx_options)

Returns an entity by ID. This is really just a shorthand for Key(cls, id).get().

Arguments

id A string or integer key ID. parent Parent key of the model to get.

app (keyword arg) ID of app. If not specified, gets data for current app.

namespace (keyword arg) Namespace. If not specified, gets data for default namespace.

**ctx_options Context options Returns a model instance or None if not found.

I discover this new app parameter. This is what I needed since a long time ago!!!!! I just tried to access datastore of app "xxxxxdev" from app "xxxxxglobal" but i get this error:

File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/datastore/datastore_rpc.py", line 1373, in check_rpc_success
    raise _ToDatastoreError(err)
BadRequestError: app s~xxxxxglobal cannot access app xxxxxxdev's data

I added the accounts services xxxxxdev@appspot.gserviceaccount.com and xxxxxglobal@appspot.gserviceaccount.com as admin of the each other in this link: https://console.cloud.google.com/iam-admin/iam/

But I still receive the problem.

Can anyone help me? I need to know where in the control panel I can grant datastore access to other app in App Engine.


回答1:


Currently, ndb doesn't use the Cloud Datastore API and in its normal mode of operation can't connect to another app's Datastore - it connects natively to the Datastore of the app the code is running on.

You can, however, use the remote api to have ndb connect to the Datastores of different apps from the development environment.

There is currently [an open Feature Request in the Public Issue Tracker for App Engine for the possibility of accessing multiple Datastores from a single app. Feel free to post a Feature Request for ndb specifically, although from the following comments in the source it appears this is already something that's being worked-on:

 current_app_id = os.environ.get('APPLICATION_ID', None)
  if current_app_id and current_app_id != app_id:
    # TODO(pcostello): We should support this so users can connect to different
    # applications.
    raise ValueError('Cannot create a Cloud Datastore context that connects '
                     'to an application (%s) that differs from the application '
                     'already connected to (%s).' % (app_id, current_app_id))
  os.environ['APPLICATION_ID'] = app_id


来源:https://stackoverflow.com/questions/37582110/gae-ndb-datastore-new-feature-access-datastore-entities-from-other-gae-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!