Node.js Helpers
A set of classes to simplify interaction with Google Apis.
Table of Contents
Setup
-
Install Node.js v8.9.2 or greater
-
Set the
GCLOUD_PROJECT
environment variable:Linux:
export GCLOUD_PROJECT=your-project-id
Windows:
set GCLOUD_PROJECT=your-project-id
Windows (PowerShell):
$env:GCLOUD_PROJECT="your-project-id"
-
Obtain authentication credentials.
Create local credentials by running the following command and following the oauth2 flow (read more about the command here):
gcloud auth application-default login
In non-Google Cloud environments, GCE instances created without the correct scopes, or local workstations where the
gcloud beta auth application-default login
command fails, use a service account by doing the following:- Go to API Manager -> Credentials
- Click "New Credentials", and create a service account or click here
- Download the JSON for this service account, and set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to point to the file containing the JSON credentials.
Set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable:Linux:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account_file.json
Windows:
set GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account_file.json
Windows (PowerShell):
$env:GOOGLE_APPLICATION_CREDENTIALS="/path/to/service_account_file.json"
Note for code running on GCE, GAE, or other environments:
On Google App Engine, the credentials should be found automatically.
On Google Compute Engine, the credentials should be found automatically, but require that you create the instance with the correct scopes.
gcloud compute instances create --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/compute,https://www.googleapis.com/auth/compute.readonly" test-instance
If you did not create the instance with the right scopes, you can still upload a JSON service account and set
GOOGLE_APPLICATION_CREDENTIALS
as described.Read more about Google Cloud Platform Authentication.
How to use
MessageTransport
Using Promises
const MessageTransport = ; const topicName = "test-jal" data = jal: "test" attributes = attr1: "1" attr2: "2" transport = topicName;transport ;
Using async/await
const MessageTransport = ; { try const topicName = "test-jal" data = jal: "test" attributes = attr1: "1" attr2: "2" const transport = topicName; const response = await transport; console; // 45031755284181 // messageId of the published message catch err console; } async await ;;
StorageProvider
Using Promises
const StorageProvider = ; const storage = forBigQuery: true bucketName: "now-ims-core-dev_bigquery-staging"; storage ; storage ;
Using async/await
const StorageProvider = ; { try const storage = forBigQuery: true bucketName: "my-testing-bucket" ; let response = await storage; console; // { success: true, payload: '"h"\n"e"\n"y"' } response = await storage; console; // { success: true, payload: 'hey' } catch err console; } async await ;;
Logger
const Logger = ;const logger = ; logger;// 2018-02-28T05:21:53.214Z - debug: debuglogger;// 2018-02-28T05:21:53.217Z - info: infologger;// 2018-02-28T05:21:53.218Z - error: error