Skip to content

Instantly share code, notes, and snippets.

@JonCatmull
Last active January 5, 2024 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonCatmull/fa55298fd55a565f246ab2e93c5c0e4b to your computer and use it in GitHub Desktop.
Save JonCatmull/fa55298fd55a565f246ab2e93c5c0e4b to your computer and use it in GitHub Desktop.
List GCP cloud bucket files over a certain size

List bucket files over certain size node.js / Typescript

Install

Install gcloud https://cloud.google.com/sdk/docs/install

Login and set project

gcloud init
# or just authorize
gcloud auth login
# then set project to the one that contains bucket
gcloud config set project [project-name]

Install script dependencies

npm i ts-node firebase firebase-admin typescript yargs @types/node

Run

Run with ts-node and pass in bucket name and max file size (e.g. 10MB)

ts-node ./index.ts  -b your-bucket.appspot.com -m 10

Logs values to console and also creates a large-files.log file with path, url and size.

import { initializeApp } from "firebase-admin/app";
import yargs from "yargs";
import fs from "fs";
import { hideBin } from "yargs/helpers";
import { getStorage } from "firebase-admin/storage";
const argv = yargs(hideBin(process.argv))
.option("bucket", {
alias: "b",
description: "bucket to copy images to",
type: "string",
demandOption: true,
})
.option("maxSizeMB", {
alias: "m",
description: "Max file size in MB",
type: "number",
demandOption: true,
})
.parse();
// Initialize Firebase
initializeApp({
storageBucket: argv["bucket"],
});
function logError(msg: string, file = "large-files-error.log") {
console.error(msg);
return fs.promises.appendFile(file, `${msg}\n`);
}
function log(msg: string, file = "large-files.log") {
console.log(msg);
return fs.promises.appendFile(file, `${msg}\n`);
}
const MB = 1024 * 1024;
try {
const run = async () => {
const storage = getStorage();
const bucket = storage.bucket();
bucket
.getFilesStream()
.on("data", (file) => {
const size = parseInt(file.metadata.size, 10);
if (size > argv["maxSizeMB"] * MB) {
log(`${file.name}
URL: https://storage.googleapis.com/${argv["bucket"]}/${file.name}
Size: ${(size / 1024 / 1024).toFixed(2)}MB
--------------------`);
}
})
.on("error", (e) => {
console.error(e);
})
.on("end", () => {
console.log("done");
});
};
run();
} catch (e) {
logError(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment