How to use gcloud - 10 common examples

To help you get started, we’ve selected a few gcloud examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jasonpolites / gcf-recipes / gcs / index.js View on Github external
if (!bucketName) {
      context.failure(
        'Bucket not provided. Make sure you have a \'bucket\' property in ' +
        'your request');
      return;
    }
    if (!fileName) {
      context.failure(
        'Filename not provided. Make sure you have a \'file\' property in ' +
        'your request');
      return;
    }

    // Create a gcs client.
    var gcs = gcloud.storage({
      // We're using the API from the same project as the Cloud Function.
      projectId: process.env.GCP_PROJECT,
    });

    var bucket = gcs.bucket(bucketName);
    var file = bucket.file(fileName);
    var count = 0;

    // Use the readLine module to read the stream line-by line.
    var lineReader = readline.createInterface({
      input: file.createReadStream(),
    });

    lineReader.on('line', function(line) {
      count += line.trim().split(/\s+/).length;
    });
github googleapis / nodejs-logging / samples / export.js View on Github external
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START setup]
// By default, gcloud will authenticate using the service account file specified
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the
// project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
var gcloud = require('gcloud');

// Get a reference to the logging component
var logging = gcloud.logging();
// [END setup]

// [START listSinks]
/**
 * @param {Function} callback Callback function.
 */
function listSinksExample (callback) {
  // list all sinks in the authenticated project
  logging.getSinks(function (err, sinks) {
    if (err) {
      return callback(err);
    }

    // Should have received all sinks
    console.log('Found ' + sinks.length + ' sinks');
    callback(null, sinks);
github jasonpolites / gcf-recipes / worker_pubsub / index.js View on Github external
'worker': function(context, data) {

    // We expect the data argument to contain a 'line' property.
    var batch = data['batch'];

    // Batch should be an array.
    var count = 0;
    for (var i = 0; i < batch.length; i++) {
      var line = batch[i];
      // Just split to count words.
      count += line.split(/\s+/).length;
    }

    // Create a pubsub client to publish the result
    var pubsub = gcloud.pubsub({
      // We're using the API from the same project as the Cloud Function.
      projectId: process.env.GCP_PROJECT,
    });

    var outTopic = pubsub.topic(data['out-topic']);

    logger.log('Worker ' + data['worker'] + ' reporting total count of ' +
      count + ' in batch of size [' + batch.length + ']');

    outTopic.publish({
      data: {
        count: count,
        worker: data['worker']
      }
    }, function(err) {
      if (err) {
github jasonpolites / gcf-recipes / bigquery / app / index.js View on Github external
var _getStorageClient = function() {
  if (storage === null) {
    storage = gcloud.storage({
      // We're using the API from the same project as the Cloud Function
      projectId: process.env.GCP_PROJECT,
    });
  }
  return storage;
};
github jasonpolites / gcf-recipes / worker_pubsub / index.js View on Github external
'master': function(context, data) {

    // Create a gcs client
    var gcs = gcloud.storage({
      // We're using the API from the same project as the Cloud Function.
      projectId: process.env.GCP_PROJECT,
    });

    // Create a pubsub client to publish the work and read the results of the workers.
    var pubsub = gcloud.pubsub({
      // We're using the API from the same project as the Cloud Function.
      projectId: process.env.GCP_PROJECT,
    });

    // Get the bucket containing our source file
    var bucket = gcs.bucket(data['bucket']);

    // The topic we are going to publish to
    var inTopic = pubsub.topic(data['in-topic']);
github jasonpolites / gcf-recipes / worker_http / index.js View on Github external
var _master = function(context, data) {

  // Create a gcs client
  var gcs = gcloud.storage({
    // We're using the API from the same project as the Cloud Function.
    projectId: process.env.GCP_PROJECT,
  });

  // Get the location (url) of the map function
  var fnUrl = data['workerFunctionUrl'];

  // Get the bucket containing our source file
  var bucket = gcs.bucket(data['bucket']);

  // Load the master file using the stream API
  logger.log(
    'Opening file [' + data['file'] + '] and creating a read stream...');
  var inStream = bucket.file(data['file']).createReadStream()
    .on('error', function(err) {
      context.failure('Error reading file stream for ' + data['file'] +
github jasonpolites / gcf-recipes / bigquery / app / index.js View on Github external
var _getBQClient = function() {
  if (bigquery === null) {
    bigquery = gcloud.bigquery({
      // We're using the API from the same project as the Cloud Function
      projectId: process.env.GCP_PROJECT,
    });
  }
  return bigquery;
};
github googleapis / nodejs-logging / samples / write.js View on Github external
/* jshint camelcase:false */
'use strict';

var async = require('async');

// [START write]
// [START setup]
// By default, gcloud will authenticate using the service account file specified
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the
// project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
var gcloud = require('gcloud');

// Get a reference to the logging component
var logging = gcloud.logging();
// [END setup]

/**
 * @param {string} logName Name of the log to write to.
 * @param {Function} callback Callback function.
 */
function writeExample (logName, callback) {
  // Get a reference to an existing log
  var log = logging.log(logName);

  // Modify this resource type to match a resource in your project
  // See https://cloud.google.com/logging/docs/api/ref_v2beta1/rest \
  //       /v2beta1/monitoredResourceDescriptors/list
  var resource = {
    type: 'gae_app',
    // This example targets a "App Engine" resource in the default module with
github googleapis / nodejs-logging / samples / list.js View on Github external
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START list]
// [START auth]
// By default, gcloud will authenticate using the service account file specified
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the
// project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
var gcloud = require('gcloud');

// Get a reference to the logging component
var logging = gcloud.logging();
// [END auth]

/**
 * @param {Object} [options] Configuration options for the request.
 * @param {Function} callback Callback function.
 */
function listExample (options, callback) {
  if (typeof options === 'function') {
    callback = options;
  }

  // Retrieve the latest some log entries from the authenticated project.
  logging.getEntries(options, function (err, entries, nextQuery, apiResponse) {
    if (err) {
      return callback(err);
    }
github jasonpolites / gcf-recipes / pubsub / index.js View on Github external
context.failure(
        'Topic not provided. Make sure you have a \'topic\' property in ' +
        'your request');
      return;
    }
    if (!message) {
      context.failure(
        'Message not provided. Make sure you have a \'message\' property ' +
        'in your request');
      return;
    }

    logger.log('Publishing message to topic ' + topicName);

    // Create a pubsub client.
    var pubsub = gcloud.pubsub({
      // We're using the API from the same project as the Cloud Function.
      projectId: process.env.GCP_PROJECT,
    });

    // The Pub/Sub topic must already exist.
    var topic = pubsub.topic(topicName);

    // Pub/Sub messages must be valid JSON objects.
    topic.publish({
        data: {
          message: message,
        },
      },
      function(err) {
        if (err) {
          logger.error(err);

gcloud

Google Cloud APIs Client Library for Node.js

Apache-2.0
Latest version published 7 years ago

Package Health Score

69 / 100
Full package analysis