thiago-s
6/14/2017 - 10:55 PM

#node.js #firebase #google-cloud #snippets

#node.js #firebase #google-cloud #snippets

var storage = require('@google-cloud/storage');
var fs = require('fs');

var bucketName = "friendlychat-890e0.appspot.com";

// authentication, if needed
// var gcs = storage({
//   projectId: 'friendlychat-890e0',
//   keyFilename: '/Users/thiagosouza/.ssh/FriendlyChat-89ba1a2db93d.json'
// });

// Instantiates a client
var gcs = storage();

// Lists files in the bucket
gcs
  .bucket(bucketName)
  .getFiles()
  .then((results) => {
    const files = results[0];

    console.log('Files:');
    files.forEach((file) => {
      console.log(file.name);
    });
  })
  .catch((err) => {
    console.error('ERROR:', err);
  });
{
  "name": "google-cloud-storage",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@google-cloud/datastore": "^1.0.2",
    "@google-cloud/storage": "^1.1.1",
    "firebase": "^4.1.2",
    "firebase-admin": "^5.0.0",
    "google-cloud": "^0.55.0"
  }
}
var firebase = require('firebase');
var admin = require("firebase-admin");

// Fetch the service account key JSON file contents
var serviceAccount = require("/Users/thiagosouza/.ssh/friendlychat-890e0-firebase-adminsdk-683gc-9bbc14166f.json");
// https://console.firebase.google.com/project/friendlychat-890e0/settings/serviceaccounts/adminsdk

// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://friendlychat-890e0.firebaseio.com"
});

// As an admin, the app has access to read and write all data, regardless of Security Rules
var db = admin.database();
var ref = db.ref("messages");
ref.once("value", function(snapshot) {
  console.log(snapshot.val());
});