Use Google Cloud Client Libraries to store files, save entities, and log data

By Omar Ayoub, Product Manager

To develop a cloud application, you usually need to access an online object storage, a scalable NoSQL database and a logging infrastructure. To that end, Google Cloud Platform (GCP) provides the Cloud Storage API, the Cloud Datastore API, and the Stackdriver Logging API. Better yet, you can now access those APIs via the latest Google Cloud Client Libraries which we’re proud to announce, are now Generally Available (GA) in seven server-side languages: C#, Go, Java, Node.js, PHP, Python and Ruby.

Online object storageFor your object storage needs, the Cloud Storage API enables you for instance to upload blobs of data, such as picture or movies, directly into buckets. To do so in Node.js for example, you first need to install the Cloud Client Library:

npm install –save @google-cloud/storage
and then simply run the following code to upload a local file into a specific bucket:

const Storage = require(‘@google-cloud/storage’);

// Instantiates a client
const storage = Storage();

// References an existing bucket, e.g. “my-bucket”
const bucket = storage.bucket(bucketName);

// Upload a local file to the bucket, e.g. “./local/path/to/file.txt”
return bucket.upload(fileName)
.then((results) => {
const file = results[0];
console.log(`File ${file.name} uploaded`);
});

NoSQL DatabaseWith Cloud Datastore, one of our NoSQL offerings, you can create entities, which are structured objects, and save them in GCP so that they can be retrieved or queried by your application at a later time. Here’s an example in Java, where you specify the maven dependency in the following manner:

com.google.cloud
google-cloud-datastore
1.0.0
followed by executing this code to create a task entity:

// Imports the Google Cloud Client Library
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Key;

public class QuickstartSample {
public static void main(String… args) throws Exception {

// Instantiates a client
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();

// The kind for the new entity
String kind = “Task”;

// The name/ID for the new entity
String name = “sampletask1″;

// The Cloud Datastore key for the new entity
Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name);

// Prepares the new entity
Entity task = Entity.newBuilder(taskKey)
.set(“description”, “Buy milk”)
.build();

// Saves the entity
datastore.put(task);
}
}
Logging frameworkOur libraries also allow you to send log data and events very easily to the Stackdriver Logging API. As a Python developer for instance, the first step is to install the Cloud Client Library for Logging:

pip install –upgrade google-cloud-logging
Then add the following code to your project (e.g. your __init__.py file):

import logging
import google.cloud.logging
client = google.cloud.logging.Client()
# Attaches a Google Stackdriver logging handler to the root logger
client.setup_logging(logging.INFO)
Then, just use the standard Python logging module to directly report logs to Stackdriver Logging:

import logging
logging.error(‘This is an error’)
We encourage you to visit the client libraries page for Cloud Storage, Cloud Datastore and Stackdriver Logging to learn more on how to get started programmatically with these APIs across all of the supported languages. To see the full list of APIs covered by the Cloud Client Libraries, or to give us feedback, you can also visit the respective GitHub repositories in the Google Cloud Platform organization.
Quelle: Google Cloud Platform

Published by