Google Cloud Node.js Client
Node.js idiomatic client for Google Cloud Platform services.
This client supports the following Google Cloud Platform services:
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.
var projectId = process.env.GCLOUD_PROJECT_ID;
var gcloud = require('gcloud')({
projectId: projectId
});
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.
var projectId = process.env.GCLOUD_PROJECT_ID;
var gcloud = require('gcloud')({
projectId: projectId,
keyFilename: '/path/to/keyfile.json'
});
You can also set auth on a per-API-instance basis. The examples below show you how.
Google BigQuery
Preview
var gcloud = require('gcloud');
var bigquery = gcloud.bigquery({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
var schoolsDataset = bigquery.dataset('schools');
schoolsDataset.import('/local/file.json', function(err, job) {});
var job = bigquery.job('job-id');
job.getQueryResults(function(err, rows) {});
job.getQueryResults().on('data', function(row) {});
Google Cloud Datastore
Follow the activation instructions to use the Google Cloud Datastore API with your project.
Preview
var gcloud = require('gcloud');
var dataset = gcloud.datastore.dataset({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
dataset.get(dataset.key(['Product', 'Computer']), function(err, entity) {
console.log(err || entity);
});
var blogPostData = {
title: 'How to make the perfect homemade pasta',
author: 'Andrew Chilton',
isDraft: true
};
var blogPostKey = dataset.key('BlogPost');
dataset.save({
key: blogPostKey,
data: blogPostData
}, function(err) {
blogPostData.isDraft = false;
dataset.save({
key: blogPostKey,
data: blogPostData
}, function(err) {
if (!err) {
}
});
});
Google Cloud DNS
Preview
var gcloud = require('gcloud');
var dns = gcloud.dns({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
dns.createZone('my-new-zone', {
dnsName: 'my-domain.com.'
}, function(err, zone) {});
var zone = dns.zone('my-existing-zone');
var nsRecord = zone.record('ns', {
ttl: 86400,
name: 'my-domain.com.',
data: 'ns-cloud1.googledomains.com.'
});
zone.addRecord(nsRecord, function(err, change) {});
zone.export('/zonefile.zone', function(err) {});
Google Cloud Pub/Sub
Preview
var gcloud = require('gcloud');
var pubsub = gcloud.pubsub({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
var topic = pubsub.topic('my-topic');
topic.publish({
data: 'New message!'
}, function(err) {});
topic.subscribe('new-subscription', function(err, subscription) {
function onError(err) {}
function onMessage(message) {}
subscription.on('error', onError);
subscription.on('message', onMessage);
subscription.removeListener('message', onMessage);
subscription.removeListener('error', onError);
});
Google Cloud Storage
Preview
var fs = require('fs');
var gcloud = require('gcloud');
var gcs = gcloud.storage({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
gcs.createBucket('my-new-bucket', function(err, bucket) {
if (!err) {
}
});
var bucket = gcs.bucket('my-existing-bucket');
bucket.upload('/photos/zoo/zebra.jpg', function(err, file) {
if (!err) {
}
});
bucket.file('giraffe.jpg').download({
destination: '/photos/zoo/giraffe.jpg'
}, function(err) {});
var remoteReadStream = bucket.file('giraffe.jpg').createReadStream();
var localWriteStream = fs.createWriteStream('/photos/zoo/giraffe.jpg');
remoteReadStream.pipe(localWriteStream);
var localReadStream = fs.createReadStream('/photos/zoo/zebra.jpg');
var remoteWriteStream = bucket.file('zebra.jpg').createWriteStream();
localReadStream.pipe(remoteWriteStream);
Google Compute Engine
Preview
var gcloud = require('gcloud');
var gce = gcloud.compute({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
var zone = gce.zone('us-central1-a');
var name = 'ubuntu-http';
zone.createVM(name, { os: 'ubuntu' }, function(err, vm, operation) {
operation.onComplete(function(err, metadata) {
if (!err) {
}
});
});
Google Prediction API
Preview
var gcloud = require('gcloud');
var prediction = gcloud.prediction({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
prediction.getModels(function(err, models) {
if (!err) {
}
});
var model = prediction.model('my-existing-model');
model.train('english', 'Hello from your friends at Google!', function(err) {});
model.query('Hello', function(err, results) {
if (!err) {
}
});
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.
var gcloud = require('gcloud')({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
var logging = gcloud.logging();
var gcs = gcloud.storage();
logging.createSink('my-new-sink', {
destination: gcs.bucket('my-sink')
}, function(err, sink) {});
var syslog = logging.log('syslog');
var resource = {
type: 'gce_instance',
labels: {
zone: 'global',
instance_id: 3
}
};
var entry = syslog.entry(resource, {
delegate: process.env.user
});
syslog.critical(entry, function(err) {});
logging.getEntries(function(err, entries) {
if (!err) {
}
});
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 = require('gcloud');
var resource = gcloud.resource({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
resource.getProjects(function(err, projects) {
if (!err) {
}
});
var project = resource.project();
project.getMetadata(function(err, metadata) {
});
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 = require('gcloud');
var search = gcloud.search({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
var index = search.index('memberData');
var document = index.document('member-id-34211');
document.addField('preferredContactForm').addTextValue('phone');
index.createDocument(document, function(err, document) {
console.log(err || document);
});
var index = search.index('memberData');
index.search('preferredContactForm:phone')
.on('error', console.error)
.on('data', function(document) {
})
.on('end', function() {
});
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.