GAE NDB Datastore new feature: Access Datastore en

2020-05-24 03:12发布

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条回答
forever°为你锁心
2楼-- · 2020-05-24 03:44

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
查看更多
登录 后发表回答