Created
August 26, 2015 03:31
-
-
Save dearcodes/182d9842f2fda8c3d16b to your computer and use it in GitHub Desktop.
Creates a file in Azure Blob Storage using multiplarty and azure-storage packages.
This file contains 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 express from 'express'; | |
import multiparty from 'multiparty'; | |
import mongoose from 'mongoose'; | |
var router = express.Router(); | |
router.post('/uploadCourseImage', uploadFileToBlob); | |
function uploadFileToBlob(req, res) { | |
var accessKey = ''; | |
var storageAccount = 'afiliadosricos'; | |
var blobService = azure.createBlobService(storageAccount, accessKey); | |
var container = 'cdn'; | |
let filename = ''; | |
var form = new multiparty.Form(); | |
form.on('part', (part) => { | |
if (part.filename) { | |
var size = part.byteCount - part.byteOffset; | |
//to get pseudo unique value in filename | |
var name = mongoose.Types.ObjectId().toString()+ '.' + part.filename.split('.').pop(); | |
blobService.createBlockBlobFromStream(container, name, part, size,{}, (error, result) => { | |
if (error) { | |
res.send(' Blob create: error '); | |
} | |
this.filename = result; | |
}); | |
} else { | |
form.handlePart(part); | |
} | |
}); | |
form.parse(req); | |
res.status(200).json({success: true, filename}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment