Created
August 30, 2017 11:11
-
-
Save EECOLOR/c3079a59ceab9ab00bac6bfbf15047b2 to your computer and use it in GitHub Desktop.
Revisions
-
EECOLOR created this gist
Aug 30, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ 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}` }