Created
May 9, 2020 20:17
-
-
Save clairechabas/ce625de3265b6763a49adf1fa10169c7 to your computer and use it in GitHub Desktop.
File upload API endpoint handling uploads to Firebase Cloud Storage | #NodeJS #ExpressJS #Multer #Firebase #GCloudStorage #GCP
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
require('dotenv').config(); | |
const express = require('express'); | |
const app = express(); | |
const cors = require('cors'); | |
const bodyParser = require('body-parser'); | |
const { Storage } = require('@google-cloud/storage'); | |
const multer = require('multer'); | |
const port = process.env.API_PORT || 8080; | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(cors()); | |
app.get('/', (req, res) => res.send('Welcome to this file upload API :)')); | |
// Create new storage instance with Firebase project credentials | |
const storage = new Storage({ | |
projectId: process.env.GCLOUD_PROJECT_ID, | |
keyFilename: process.env.GCLOUD_APPLICATION_CREDENTIALS, | |
}); | |
// Create a bucket associated to Firebase storage bucket | |
const bucket = storage.bucket(process.env.GCLOUD_STORAGE_BUCKET_URL); | |
// Initiating a memory storage engine to store files as Buffer objects | |
const uploader = multer({ | |
storage: multer.memoryStorage(), | |
limits: { | |
fileSize: 5 * 1024 * 1024, // limiting files size to 5 MB | |
}, | |
}); | |
// Upload endpoint to send file to Firebase storage bucket | |
app.post('/api/upload', uploader.single('image'), async (req, res, next) => { | |
try { | |
if (!req.file) { | |
res.status(400).send('Error, could not upload file'); | |
return; | |
} | |
// Create new blob in the bucket referencing the file | |
const blob = bucket.file(req.file.originalname); | |
// Create writable stream and specifying file mimetype | |
const blobWriter = blob.createWriteStream({ | |
metadata: { | |
contentType: req.file.mimetype, | |
}, | |
}); | |
blobWriter.on('error', (err) => next(err)); | |
blobWriter.on('finish', () => { | |
// Assembling public URL for accessing the file via HTTP | |
const publicUrl = `https://firebasestorage.googleapis.com/v0/b/${ | |
bucket.name | |
}/o/${encodeURI(blob.name)}?alt=media`; | |
// Return the file name and its public URL | |
res | |
.status(200) | |
.send({ fileName: req.file.originalname, fileLocation: publicUrl }); | |
}); | |
// When there is no more data to be consumed from the stream | |
blobWriter.end(req.file.buffer); | |
} catch (error) { | |
res.status(400).send(`Error, could not upload file: ${error}`); | |
return; | |
} | |
}); | |
app.listen(port, () => | |
console.log(`File uploader API listening on port ${port}`) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I did the same thing, but, for some reason, when i try to see my file at google console, it doesn't load...
I figured out what was wrong!
link