Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'gcloud' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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;
});
// 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);
'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) {
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;
};
'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']);
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'] +
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;
};
/* 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
// 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);
}
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);