npm i busboy azure-storage --save
In your Express Generator app/controllers/home.js: (Note that this is just a basic example without login)
... const Busboy = require('busboy'); const blobStorage = require('../services/blobStorage'); ... router.post('/upload', function(req, res) { var busboy = new Busboy({ headers: req.headers }); busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { blobStorage.saveToBlob(filename, file, function(err, result){ if(err) { res.setEncoding(500, err); } else { res.redirect('/show?name=' + encodeURI(filename)); } }); }); req.pipe(busboy); }); router.get('/show', function (req, res) { res.render('show', { name: req.query.name, url: blobStorage.getUrl(req.query.name) }); });
In blobStorage.js | Note, you should not hardcode your details like this, they should be retrieved from Azure directly.
process.env['AZURE_STORAGE_ACCOUNT'] = 'your-storage-name'; process.env['AZURE_STORAGE_ACCESS_KEY'] = 'your-storage-access-key'; var azure = require('azure-storage'); var blobService = azure.createBlobService(); var images = 'images'; function saveToBlob(name, stream, cb) { // cb is callback stream.pipe(blobService.createWriteStreamToBlockBlob(images, name, cb)); } function getUrl(name) { var startDate = new Date(); startDate.setMinutes(startDate.getMinutes() - 15); var expiryDate = new Date(startDate); expiryDate.setMinutes(startDate.getMinutes() + 30); var permissions = azure.BlobUtilities.SharedAccessPermissions.READ; var sharedAccessPolicy = { AccessPolicy: { Permissions: azure.BlobUtilities.SharedAccessPermissions.READ, Start: startDate, Expiry: expiryDate }, }; var sasToken = blobService.generateSharedAccessSignature(images, name, sharedAccessPolicy); return blobService.getUrl(images, name, sasToken); } module.exports = { saveToBlob: saveToBlob, getUrl: getUrl };
In your .pug form
form(action="/upload", method="post", enctype="multipart/form-data") input(type="file", name="image") button(type="submit", value="Upload") Upload photo
In your .pug redirected page
p Showing #{name} img(src=url)