如何将数据插入VM实例中不同项目的GCP数据存储中?
您好,我是 GCP 和 python 的新手,我有一个要求: 我有 2 个项目project_1 和project_2,我正在project_1 的VM 实例中运行python 代码,并希望将数据插入project_2 的数据存储中。我不知道如何在 python 代码中设置project_2。 如果有任何代码将数据发送到不同项目的数据存储,那将会很有帮助。 我正在使用此代码,它将数据插入到project_1 的数据存储中。 请帮帮我。 提前致谢
from google.cloud import datastore
# Create, populate and persist an entity with keyID=1234
client = datastore.Client()
key = client.key('EntityKind', 1234)
entity = datastore.Entity(key=key)
entity.update({
'foo': u'bar',
'baz': 1337,
'qux': False,
})
client.put(entity)
# Then get by key for this entity
result = client.get(key)
print(result)
Hi I am new to GCP and python, I have one requirement:
I have 2 projects project_1 and project_2, I am running python code in project_1's VM instance and want to insert data into project_2's Datastore. I dont know how to set project_2 in the python code.
If there is any code which sends data to different project's datastore that would be helpful.
I am using this code and it is inserting data into project_1's datastore.
Please help me out.
Thanks in advance
from google.cloud import datastore
# Create, populate and persist an entity with keyID=1234
client = datastore.Client()
key = client.key('EntityKind', 1234)
entity = datastore.Entity(key=key)
entity.update({
'foo': u'bar',
'baz': 1337,
'qux': False,
})
client.put(entity)
# Then get by key for this entity
result = client.get(key)
print(result)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题将受益于展示一些寻找解决方案的过程。
Google 搜索“
datastore.Client
” ,返回描述参数的包装器的 API 文档:您将需要使用
project
参数指定其他项目。您需要确保您使用的服务帐号(可能是 Compute Engine 默认服务帐号,但您应该考虑为您的代码创建特定的服务帐号)包含适当的 IAM 角色|读取|写入数据存储的权限,可能是
roles/datastore.user
。请参阅此教程,了解如何开始使用使用 Python 的数据存储云客户端库。
Your question would benefit from showing some in finding the solution.
A Google search for "
datastore.Client
", returns the API documentation for the wrapper describing the parameters:You will want to specify the other project using the
project
argument.You will need to ensure that the Service Account that you're using (probably the Compute Engine default Service Account but you should consider creating a specific Service Account for your code), includes appropriate IAM roles|permissions to read|write to Datastore, perhaps
roles/datastore.user
.See this tutorial for getting started with the Cloud Client Libraries for Datastore using Python.