This page discusses how you can get started with Google Cloud Datastore using
Ruby. Follow the steps in Sign Up to make sure you are ready to run
this example. After activation,
you will have a
<dataset-id> (same identifier as your
Google Cloud Project ID) that you need to run the code.
Step 1: Set up your environment
In this step we'll show you how to create a Google Compute Engine (GCE) instance where you can run this example. If you already have an environment where you can run the code, go to Step 2.
When you set up a GCE instance, make sure it has the scopes
userinfo-email so that you can access Google Cloud Datastore. For example,
you can use the following
compute command to
create an instance:
$ INSTANCE_NAME=<instance-name> $ gcloud compute instances create $INSTANCE_NAME --scopes datastore userinfo-email
$ sudo apt-get update $ sudo apt-get install ruby $ curl -sSL https://get.rvm.io | bash -s stable
In order to make API calls to the Datastore, you need to install the google-api-client gem:
$ rvm use 2 $ gem install google-api-client
Step 2: Get service account credentials
Run the following commands (in a bash-like shell):
# configure your credentials export DATASTORE_SERVICE_ACCOUNT=<service-account-email> export DATASTORE_PRIVATE_KEY_FILE=<path-to-p12-private-key-file>
<service-acccount-email> is the the service account email address from
the Service accounts list for your app in the Google Developers console, in
You can get the service account credentials from the Google Developers Console.
In the Google Developers Console, under Credentials, you can create a new credential or access existing credentials.
To retrieve the private p12 key, take one of the following actions:
- If the service account already exists and you have the private p12 key, you can use that key.
- If the service account already exists and you do not have the private key, you can generate a new key and download it. Any applications using the existing private keys associated with this service account will continue to work.
- Create a new service account and download the private p12 key.
You need to put the private p12 key in a location that you can reference from the code. For more information about service accounts, see Service Account Authentication.
Step 3: Review the demo code file
The code for the demo is in a file called
adams.rb file. The comments in the
sample's source explain its behavior in detail:
Step 4: Run the code
Download and unzip the latest version of the
$ unzip google-cloud-datastore-1beta2-rev1-2.1.1.zip
adams.rb demo with your
<dataset-id> as a parameter.
It will prompt for the question and validate your answer.
$ ruby google-cloud-datastore-1beta2-rev1-2.1.1/ruby/demos/trivial/adams.rb <dataset-id> Meaning of life? > 11 Don't Panic!
With this example, you learned how to use the:
google-api-clientgem to connect to the Datastore API.
begin_transactionmethod to start a transaction.
lookupmethod to retrieve entities by key from your dataset.
commitmethod to send mutations to entities in your dataset and commit the transaction.