Created
July 25, 2018 18:06
-
-
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
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
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; |
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
Thank you so much it works perfect.