Skip to content

Instantly share code, notes, and snippets.

@ovaris
Last active March 17, 2020 18:09
Show Gist options
  • Save ovaris/c30c492db06fbe73875ed71d00dcb547 to your computer and use it in GitHub Desktop.
Save ovaris/c30c492db06fbe73875ed71d00dcb547 to your computer and use it in GitHub Desktop.
Google cloud storage problem
import {chunk} from 'lodash';
import * as admin from "firebase-admin";
import * as storage from '@google-cloud/storage';
const gcs = storage();
//https://github.com/GoogleCloudPlatform/google-cloud-node/issues/2254
gcs.interceptors.push({
request: function(reqOpts) {
reqOpts.forever = false;
return reqOpts
}
});
const ENV = process.env.ENV || "dev";
// Fetch the service account key JSON file contents
const serviceAccount = require(`../../../xxxx.json`);
// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: `https://${serviceAccount.project_id}.firebaseio.com/`
});
const bucket = gcs.bucket('xxxx');
const checkFileExists = (fileName) =>
bucket.file(fileName).exists()
.then(([exists]) => exists || fileName)
.catch((error) => {
console.error('Error checking ', fileName);
console.error(error);
return `error: ${fileName}`;
});
(async () => {
const data = admin.database().ref('some_collection');
const snapshot = await data.once('value');
const checkPromises = [];
snapshot.forEach((data) => {
const cloth = data.val();
const id = cloth.id;
checkPromises.push(checkFileExists(`optimized_png/${id}.png`));
checkPromises.push(checkFileExists(`optimized/${id}.webp`));
checkPromises.push(checkFileExists(`optimized/${id}@2x.webp`));
checkPromises.push(checkFileExists(`optimized/${id}@3x.webp`));
});
const chunks = chunk(checkPromises, 20);
let i = 1;
let results = [];
for (const chunk of chunks) {
const chunkResults = await Promise.all(chunk);
results = results.concat(chunkResults);
i++;
}
const missing = results.filter((exists) => exists !== true);
if (missing.length > 0) {
console.log('Missing: \n');
missing.forEach((file, i) => console.log(`${i}: ${file}`));
process.exit(1);
}
console.log('YAY!');
process.exit(0);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment