def _make_cloud_datastore_context(app_id, external_app_ids=()):
from . import model  <EOL>if not datastore_pbs._CLOUD_DATASTORE_ENABLED:<EOL><INDENT>raise datastore_errors.BadArgumentError(<EOL>datastore_pbs.MISSING_CLOUD_DATASTORE_MESSAGE)<EOL><DEDENT>import googledatastore<EOL>try:<EOL><INDENT>from google.appengine.datastore import cloud_datastore_v1_remote_stub<EOL><DEDENT>except ImportError:<EOL><INDENT>from google3.apphosting.datastore import cloud_datastore_v1_remote_stub<EOL><DEDENT>current_app_id = os.environ.get('<STR_LIT>', None)<EOL>if current_app_id and current_app_id != app_id:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % (app_id, current_app_id))<EOL><DEDENT>os.environ['<STR_LIT>'] = app_id<EOL>id_resolver = datastore_pbs.IdResolver((app_id,) + tuple(external_app_ids))<EOL>project_id = id_resolver.resolve_project_id(app_id)<EOL>endpoint = googledatastore.helper.get_project_endpoint_from_env(project_id)<EOL>datastore = googledatastore.Datastore(<EOL>project_endpoint=endpoint,<EOL>credentials=googledatastore.helper.get_credentials_from_env())<EOL>conn = model.make_connection(_api_version=datastore_rpc._CLOUD_DATASTORE_V1,<EOL>_id_resolver=id_resolver)<EOL>try:<EOL><INDENT>stub = cloud_datastore_v1_remote_stub.CloudDatastoreV1RemoteStub(datastore)<EOL>apiproxy_stub_map.apiproxy.RegisterStub(datastore_rpc._CLOUD_DATASTORE_V1,<EOL>stub)<EOL><DEDENT>except:<EOL><INDENT>pass  <EOL><DEDENT>try:<EOL><INDENT>apiproxy_stub_map.apiproxy.RegisterStub('<STR_LIT>', _ThrowingStub())<EOL><DEDENT>except:<EOL><INDENT>pass  <EOL><DEDENT>try:<EOL><INDENT>apiproxy_stub_map.apiproxy.RegisterStub('<STR_LIT>', _ThrowingStub())<EOL><DEDENT>except:<EOL><INDENT>pass  <EOL><DEDENT>return make_context(conn=conn)<EOL>
Creates a new context to connect to a remote Cloud Datastore instance.

    This should only be used outside of Google App Engine.

    Args:
      app_id: The application id to connect to. This differs from the project
        id as it may have an additional prefix, e.g. "s~" or "e~".
      external_app_ids: A list of apps that may be referenced by data in your
        application. For example, if you are connected to s~my-app and store keys
        for s~my-other-app, you should include s~my-other-app in the external_apps
        list.
    Returns:
      An ndb.Context that can connect to a Remote Cloud Datastore. You can use
      this context by passing it to ndb.set_context.