Integration with Google Cloud Storage

What You'll Need

Backbench account - Sign Up, the personal account will always remain free to use.

Google developer login- create an account and create an app on google developers from https://console.developers.google.com.

Prerequisites

  1. Setting up project: after creating a project in google developers, select Api’s and services under that library tab, you will find Api library for different categories, select storage under that select google cloud storage, you will have to click on enable button, this will enable google cloud storage to your project.

  2. Setting up environment variables: Create a Genomics-enabled project via the Google Cloud Platform Console (The Cloud Storage JSON API must also be enabled.) and create a service account.

  • Once service account is created go to credentials page and download the credentials.

  • Now go to backbench platform and create a module with .json extension, say cloud.json. copy paste the downloaded credentials and save.

  • Now go to environment variable section of backbench and add a new environment variable with GOOGLE_APPLICATION_CREDENTIALS as name and value with the path of json file as ./cloud.json and hit on add.

Adding Dependencies

For nodejs only.

Create a module name package.json and paste the following code.

{
	"dependencies": {
		"@google-cloud/storage": "1.6.0"
	}
}

Operation 1: Creating bucket.

Backend

  1. Select +, in the upper right corner to create a Module. For example, say "google_create.js" and select CREATE or hit Enter.

  2. Copy and paste the code module from below.

  3. Replace PROJECT_ID (from google developers dashboard).

  4. Select save.

// Imports the Google Cloud client library
const Storage = require("@google-cloud/storage");

// Your Google Cloud Platform project ID
const projectId = "PRODUCT_ID";

// Creates a client
const storage = new Storage({
  projectId: projectId,
});

// The name for the new bucket
const bucketName = "my_bucket";

module.exports.endpoint= function(req,cb) {
// Creates the new bucket
storage
  .createBucket(bucketName)
  .then(() => {
    console.log("Bucket created.");
     cb(undefined,{d: "bucket created"});
  })
  .catch(err => {
    console.error("ERROR:", err);
  cb(err);

  }); 
};

Operation 2: Uploading file in a storage bucket

Backend

  1. Select +, in the upper right corner to create a Module. For example, say "Uploading_object.js" and select CREATE or hit Enter.

  2. Copy and paste the code module from below.

  3. Replace PROJECT_ID (from google developers dashboard).

  4. Select save.

// Imports the Google Cloud client library
const Storage = require("@google-cloud/storage");

// Your Google Cloud Platform project ID    
const projectId = "PROJECT_ID";

// Creates a client
const storage = new Storage({
  projectId: projectId,
});
// The name for the new bucket
const bucketName = "my_bucket";
const filename = "FILE_NAME";

module.exports.endpoint= function(req,cb) {
   storage
  .bucket(bucketName)
  .upload(filename)
  .then(() => {
    console.log("${filename} uploaded to ${bucketName}.");
  cb(undefined,{d: "file uploaded"}); 
  })
  .catch(err => {
    console.error("ERROR:", err);
    cb(err);
  });
};

Operation 3: Listing objects in a storage bucket.

Backend

  1. Select +, in the upper right corner to create a Module. For example, say "listing_object.js" and select CREATE or hit Enter.

  2. Copy and paste the code module from below.

  3. Replace PROJECT_ID (from google developers dashboard).

  4. Select save.

// Imports the Google Cloud client library
const Storage = require("@google-cloud/storage");
// Your Google Cloud Platform project ID
const projectId = "PROJECT_ID";
// Creates a client
const storage = new Storage({
  projectId: projectId, 
});
// The name for the new bucket
const bucketName = "my_bucket";
module.exports.endpoint= function (req, cb) {
storage
  .bucket(bucketName)
  .getFiles()
  .then(results => { 
    const files = results[0];
    console.log("Files:");
    files.forEach(file => {
      console.log(file.name);
          cb(undefined,files);
    });
   // cb(undefined, {data : results})
  })
  .catch(err => {
      cb({err: err})
    console.error("ERROR:", err);
  });
};

Operation 3: Deleting objects in a storage bucket.

Backend

  1. Select +, in the upper right corner to create a Module. For example, say "deleting.js" and select CREATE or hit Enter.

  2. Copy and paste the code module from below.

  3. Replace PROJECT_ID (from google developers dashboard).

  4. Select save.

// Imports the Google Cloud client library
const Storage = require("@google-cloud/storage");
// Your Google Cloud Platform project ID
const projectId = "PROJECT_ID";
// Creates a client
const storage = new Storage({
  projectId: projectId,
});
// The name for the new bucket
const bucketName = "my_bench";
const filename = "FILE_NAME";
module.exports.endpoint= function(req,cb) {
    storage
  .bucket(bucketName)
  .file(filename)
  .delete()
  .then(() => {
    console.log(“deleted.");
    cb(undefined, {d: "File Deleted"})
  })
  .catch(err => {
    console.error("ERROR:", err);
    cb(err);
  });
};

Last updated