Google ML Engine和Python数据存储API,'禁止访问:403请求的身份验证范围不足。'

时间:2017-05-13 12:04:31

标签: python google-app-engine google-cloud-datastore google-cloud-platform google-cloud-ml-engine

我正在Google ML Engine上运行TensorFlow模型。模型训练完成后,我想将结果的JSON字符串存储到数据存储区。为此,我使用以下内容:

from gcloud import datastore

def put_json_into_datastore(json_str, project_id, entity_type):
    """
    Store json string in Datastore
    """
    # Instantiate the client to the project
    datastore_client = datastore.Client(project_id)
    # The name/ID for the new entity
    name = str(datetime.datetime.now())
    # The Cloud Datastore key for the new entity
    entity_key = datastore_client.key(entity_type, name)
    # Prepare the new entity
    entity = datastore.Entity(key=entity_key)
    # Get the json string into the entity
    entity.update(json_str)
    # Put the entity into Datastore
    datastore_client.put(entity)

虽然,我收到错误'禁止访问:403请求的身份验证范围不足。'这是完整的错误跟踪:

  

Traceback(最近一次调用最后一次):文件   “/usr/lib/python2.7/runpy.py”,第162行,在_run_module_as_main中       “ main ”,fname,loader,pkg_name)文件“/usr/lib/python2.7/runpy.py”,第72行,在_run_code中       run_globals中的exec代码文件“/root/.local/lib/python2.7/site-packages/trainer/train.py”,第243行,   在       FLAGS.entity_type)文件“/root/.local/lib/python2.7/site-packages/trainer/data_helpers.py”,   第253行,在put_json_into_datastore中       datastore_client.put(entity)文件“/usr/local/lib/python2.7/dist-packages/gcloud/datastore/client.py”,   第329行,放入       self.put_multi(entities = [entity])File“/usr/local/lib/python2.7/dist-packages/gcloud/datastore/client.py”,   第355行,在put_multi中       current.commit()文件“/usr/local/lib/python2.7/dist-packages/gcloud/datastore/batch.py​​”,   第260行,在提交中       self._commit()文件“/usr/local/lib/python2.7/dist-packages/gcloud/datastore/batch.py​​”,   第243行,在_commit中       self.project,self._commit_request,self._id)文件“/usr/local/lib/python2.7/dist-packages/gcloud/datastore/connection.py”,   第342行,在提交中       _datastore_pb2.CommitResponse)文件“/usr/local/lib/python2.7/dist-packages/gcloud/datastore/connection.py”,   第114行,在_rpc中       data = request_pb.SerializeToString())文件“/usr/local/lib/python2.7/dist-packages/gcloud/datastore/connection.py”,   第98行,在_request       raise make_exception(headers,error_status.message,use_json = False)Forbidden:403 Request认证不足   作用域。

我是否需要在ML引擎访问数据存储的地方授予访问权限?

1 个答案:

答案 0 :(得分:2)

Cloud ML服务不会以足以访问数据存储区的权限执行。解决此问题的一种方法是上载可访问Cloud Datastore的服务帐户的凭证(例如json服务帐户密钥文件)。然后,您可以使用它来获取能够访问数据存储区的凭据。