Skip to content

Instantly share code, notes, and snippets.

@JonCatmull
Last active January 5, 2024 09:13
Show Gist options
  • Select an option

  • Save JonCatmull/fa55298fd55a565f246ab2e93c5c0e4b to your computer and use it in GitHub Desktop.

Select an option

Save JonCatmull/fa55298fd55a565f246ab2e93c5c0e4b to your computer and use it in GitHub Desktop.

Revisions

  1. JonCatmull revised this gist Jan 5, 2024. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,19 @@

    ## 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
    ```
  2. JonCatmull created this gist Jan 5, 2024.
    16 changes: 16 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # List bucket files over certain size node.js / Typescript

    ## Install

    ```
    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.
    65 changes: 65 additions & 0 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    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);
    }