const storage = require('@google-cloud/storage') const createUuid = require("uuid-v4") const credentials = require('./test-firebase-credentials.json') const firebaseProjectName = 'test' const bucketName = `${firebaseProjectName}.appspot.com` const bucket = storage({ credentials }).bucket(bucketName) module.exports = firebaseSaveFile function firebaseSaveFile(fileName, contentType, data) { const file = bucket.file(fileName) return file.getMetadata().catch(e => []) .then(([m]) => ((m || {}).metadata || {}).firebaseStorageDownloadTokens) .then((token = createUuid()) => file.save(data, { validation: 'md5', resumable: false, metadata: { contentType, metadata: { firebaseStorageDownloadTokens: token } } }).then(_ => token)) .then(token => getDownloadUrl(fileName, token)) } function getDownloadUrl(fileName, token) { return `https://firebasestorage.googleapis.com/v0/b/${bucketName}/o/${fileName}?alt=media&token=${token}` }