Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Created July 25, 2018 18:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrburgess/96af739617c9940e5b558fab7814df9f to your computer and use it in GitHub Desktop.
Save wrburgess/96af739617c9940e5b558fab7814df9f to your computer and use it in GitHub Desktop.
Using Firebase Functions to create and upload a text file to Firebase Storage using Node and firebase-admin
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
import * as admin from 'firebase-admin';
const exportToFile = async (req, _, next) => {
try {
const fileName = 'test002.csv';
const tempFilePath = path.join(os.tmpdir(), fileName);
console.log({tempFilePath});
const content = 'id,firstname,lastname\n1,John,Doe\n2,Jane,Doe';
await fs.writeFileSync(tempFilePath, content);
var bucket = await admin.storage().bucket();
bucket.upload(tempFilePath, {
destination: `exports/${fileName}`,
});
next();
} catch (err) {
console.error('middlewares > exportToFile', err);
next();
};
};
export default exportToFile;
@andres-mora-vanegas
Copy link

Thank you so much it works perfect.

@bokub
Copy link

bokub commented Jul 8, 2021

await admin.storage().bucket();

Actually, you don't need to await because it's not a promise. Same for fs.writeFileSync

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment