Last active
June 29, 2021 19:51
-
-
Save Luke-zhang-04/7c8bdbb7c2dc05c7d3453b82a04312f8 to your computer and use it in GitHub Desktop.
batch upload/download files from Firebase Storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Run with `node --experimental-json-modules images.mjs` | |
| // Requires node 14+ | |
| // Dependencies are in the imports below | |
| import admin from "firebase-admin" | |
| import fetch from "node-fetch" | |
| import fs from "fs" | |
| import serviceAccount from "./adminsdk.json" | |
| admin.initializeApp({ | |
| credential: admin.credential.cert(serviceAccount), | |
| databaseURL: "https://<NAME>.firebaseio.com", | |
| storageBucket: "gs://<NAME>.appspot.com", | |
| }) | |
| const storage = admin.storage() | |
| const downloads = [] | |
| const files = (await storage.bucket().getFiles())[0] | |
| for (const item of files) { | |
| if (item.id?.endsWith("%2F")) { | |
| const dirname = `./images/${item.id.slice(0, item.id.length - "%2F".length)}` | |
| console.log(`Creating dir ${dirname}`) | |
| await fs.promises.mkdir(dirname, {recursive: true}) | |
| } | |
| } | |
| for (const item of files) { | |
| if (item.id?.endsWith("%2F")) { | |
| continue | |
| } | |
| downloads.push( | |
| (async () => { | |
| console.log(`${item.id} -> ./images/${decodeURIComponent(item.id)}`) | |
| const fileStream = fs.createWriteStream(`./images/${decodeURIComponent(item.id)}`) | |
| const res = await fetch( | |
| `https://firebasestorage.googleapis.com/v0/b/kk-cabinets.appspot.com/o/${item.id}?alt=media`, | |
| {method: "get"}, | |
| ) | |
| await new Promise((resolve, reject) => { | |
| res.body.pipe(fileStream) | |
| res.body.on("error", reject) | |
| fileStream.on("finish", resolve) | |
| }) | |
| })(), | |
| ) | |
| } | |
| await Promise.all(downloads) | |
| console.log("Finished Download") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Run with `node --experimental-json-modules images.mjs` | |
| // Requires node 14+ | |
| // Dependencies are in the imports below | |
| import admin from "firebase-admin" | |
| import glob from "glob" | |
| import serviceAccount from "./adminsdk.json" | |
| admin.initializeApp({ | |
| credential: admin.credential.cert(serviceAccount), | |
| databaseURL: "https://<NAME>.firebaseio.com", | |
| storageBucket: "gs://<NAME>.appspot.com", | |
| }) | |
| const storageBucket = admin.storage().bucket() | |
| /** @type {string[]} */ | |
| const files = await new Promise((resolve, reject) => { | |
| glob("./images/**/*", {nodir: true}, (err, files) => { | |
| return err ? reject(err) : resolve(files) | |
| }) | |
| }) | |
| const uploads = [] | |
| for (const file of files) { | |
| console.log(`Uploading ${file}`) | |
| uploads.push(storageBucket.upload(file, {destination: file.split("/").slice(2).join("/")})) | |
| } | |
| await Promise.all(uploads) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment