'How to insert data into GCP Datastore which is in different project in VM instance?

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)


Solution 1:[1]

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:

project (str) – (Optional) The project to pass to proxied API methods.
credentials (Credentials) – (Optional) The OAuth2 Credentials to use for this client.

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DazWilkin