-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
30 lines (25 loc) · 973 Bytes
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const azure = require('azure-storage')
module.exports = (connectionString, containerName) => {
const storage = azure.createBlobService(connectionString)
return (fileName, stream) => {
return new Promise((resolve, reject) => {
return storage.createContainerIfNotExists(containerName, error => {
if(error)
reject(error)
const blobName = getBlobName(fileName)
const writeStream = storage.createWriteStreamToBlockBlob(containerName, blobName)
stream.pipe(writeStream)
writeStream.on('error', reject)
writeStream.on('close', () => {
resolve({ url: getBlobUrl(containerName, blobName) })
})
})
})
}
function getBlobName(filename) {
return `${filename.substring(0, filename.lastIndexOf('.'))}-${Date.now()}${filename.substring(filename.lastIndexOf('.'))}`
}
function getBlobUrl(containerName, blobName) {
return storage.getUrl(containerName, blobName)
}
}