Google Cloud Node.js Client
Node.js idiomatic client for Google Cloud Platform services.
This client supports the following Google Cloud Platform services:
- Google BigQuery
- Google Cloud Datastore
- Google Cloud DNS
- Google Cloud Pub/Sub
- Google Cloud Storage
- Google Compute Engine
- Google Prediction API
- Google Cloud Logging (Beta)
- Google Cloud Resource Manager (Beta)
- Google Cloud Search (Alpha)
If you need support for other Google APIs, check out the Google Node.js API Client library.
Quick Start
$ npm install --save gcloud
Example Applications
- nodejs-getting-started - A sample and tutorial that demonstrates how to build a complete web application using Cloud Datastore, Cloud Storage, and Cloud Pub/Sub and deploy it to Google App Engine or Google Compute Engine.
- gcloud-node-todos - A TodoMVC backend using gcloud-node and Datastore.
- gitnpm - Easily lookup an npm package's GitHub repo using gcloud-node and Google App Engine.
- gcloud-kvstore - Use Datastore as a simple key-value store.
- hya-wave - Cloud-based web sample editor. Part of the hya-io family of products.
Authentication
With gcloud-node
it's incredibly easy to get authenticated and start using Google's APIs. You can set your credentials on a global basis as well as on a per-API basis. See each individual API section below to see how you can auth on a per-API-basis. This is useful if you want to use different accounts for different Google Cloud services.
On Google Compute Engine
If you are running this client on Google Compute Engine, we handle authentication for you with no configuration. You just need to make sure that when you set up the GCE instance, you add the correct scopes for the APIs you want to access.
// Authenticating on a global basis.var projectId = processenvGCLOUD_PROJECT_ID; // E.g. 'grape-spaceship-123'var gcloud =projectId: projectId;// ...you're good to go! See the next section to get started using the APIs.
Elsewhere
If you are not running this client on Google Compute Engine, you need a Google Developers service account. To create a service account:
- Visit the Google Developers Console.
- Create a new project or click on an existing project.
- Navigate to APIs & auth > APIs section and turn on the following APIs (you may need to enable billing in order to use these services):
- Google Cloud Datastore API
- Google Cloud Storage
- Google Cloud Storage JSON API
- Navigate to APIs & auth > Credentials and then:
- If you want to use a new service account, click on Create new Client ID and select Service account. After the account is created, you will be prompted to download the JSON key file that the library uses to authenticate your requests.
- If you want to generate a new key for an existing service account, click on Generate new JSON key and download the JSON key file.
// Authenticating on a global basis.var projectId = processenvGCLOUD_PROJECT_ID; // E.g. 'grape-spaceship-123'var gcloud =projectId: projectIdkeyFilename: '/path/to/keyfile.json';// ...you're good to go! See the next section to get started using the APIs.
You can also set auth on a per-API-instance basis. The examples below show you how.
Google BigQuery
Preview
var gcloud = ; // Authenticating on a per-API-basis. You don't need to do this if you auth on a// global basis (see Authentication section above).var bigquery = gcloud; // Access an existing dataset.var schoolsDataset = bigquery; // Import data into a dataset.schoolsDataset; // Get results from a query job.var job = bigquery; // Use a callback.job; // Or get the same results as a readable stream.job;
Google Cloud Datastore
Follow the activation instructions to use the Google Cloud Datastore API with your project.
Preview
var gcloud = ; // Authenticating on a per-API-basis. You don't need to do this if you auth on a// global basis (see Authentication section above). var dataset = gclouddatastore; dataset; // Save data to your dataset.var blogPostData = title: 'How to make the perfect homemade pasta' author: 'Andrew Chilton' isDraft: true; var blogPostKey = dataset; dataset;
Google Cloud DNS
Preview
var gcloud = ; // Authenticating on a per-API-basis. You don't need to do this if you auth on a// global basis (see Authentication section above). var dns = gcloud; // Create a managed zone.dns; // Reference an existing zone.var zone = dns; // Create an NS record.var nsRecord = zone; zone; // Create a zonefile from the records in your zone.zone;
Google Cloud Pub/Sub
Preview
var gcloud = ; // Authenticating on a per-API-basis. You don't need to do this if you// auth on a global basis (see Authentication section above). var pubsub = gcloud; // Reference a topic.var topic = pubsub; // Publish a message to the topic.// The topic will be created if it doesn't exist.topic; // Subscribe to the topic.topic;
Google Cloud Storage
Preview
var fs = ;var gcloud = ; // Authenticating on a per-API-basis. You don't need to do this if you auth on a// global basis (see Authentication section above). var gcs = gcloud; // Create a new bucket.gcs; // Reference an existing bucket.var bucket = gcs; // Upload a local file to a new file to be created in your bucket.bucket; // Download a file from your bucket.bucket; // Streams are also supported for reading and writing files.var remoteReadStream = bucket;var localWriteStream = fs;remoteReadStream; var localReadStream = fs;var remoteWriteStream = bucket;localReadStream;
Google Compute Engine
Preview
var gcloud = ; // Authenticating on a per-API-basis. You don't need to do this if you auth on a// global basis (see Authentication section above). var gce = gcloud; // Create a new VM using the latest OS image of your choice.var zone = gce;var name = 'ubuntu-http'; zone;
Google Prediction API
Preview
var gcloud = ; // Authenticating on a per-API-basis. You don't need to do this if you auth on a// global basis (see Authentication section above). var prediction = gcloud; // Get all of the trained models in your project.prediction; // Reference an existing trained model.var model = prediction; // Train a model.model; // Query a model.model;
Google Cloud Logging (Beta)
This is a Beta release of Google Cloud Logging. This API is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
// Authenticating on a global-basis. You can also authenticate on a per-API-// basis (see Authentication section above). var gcloud = projectId: 'my-project' keyFilename: '/path/to/keyfile.json'; var logging = gcloud; // Create a sink using a Bucket as a destination.var gcs = gcloud; logging; // Write a critical entry to a log.var syslog = logging; var resource = type: 'gce_instance' labels: zone: 'global' instance_id: 3 ; var entry = syslog; syslog; // Get all entries in your project.logging;
Google Cloud Resource Manager (Beta)
This is a Beta release of Google Cloud Resource Manager. This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
Preview
var gcloud = ; // Authorizing on a per-API-basis. You don't need to do this if you auth on a// global basis (see Authorization section above). var resource = gcloud; // Get all of the projects you maintain.resource; // Get the metadata from your project. (defaults to `my-project`)var project = resource; project;
Google Cloud Search (Alpha)
This is an Alpha release of Google Cloud Search. This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
Preview
var gcloud = ; // Authenticating on a per-API-basis. You don't need to do this if you auth on a// global basis (see Authentication section above). var search = gcloud; // Create a document in a new index.var index = searchindex'memberData'; var document = index;document; index; // Search an index and get the results as a readable object stream.var index = searchindex'memberData'; index ;
Contributing
Contributions to this library are always welcome and highly encouraged.
See CONTRIBUTING for more information on how to get started.
License
Apache 2.0 - See COPYING for more information.