Last active
October 9, 2017 10:03
-
-
Save leefsmp/ef15c394f676502f8f3c6e474e0f709f to your computer and use it in GitHub Desktop.
Uploading a resumable resource to Forge OSS (node.js)
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
///////////////////////////////////////////////////////// | |
// upload resource | |
// | |
///////////////////////////////////////////////////////// | |
router.post('/buckets/:bucketKey', | |
uploadSvc.uploader.any(), | |
async(req, res) => { | |
try { | |
const file = req.files[0] | |
const forgeSvc = ServiceManager.getService('ForgeSvc') | |
const ossSvc = ServiceManager.getService('OssSvc') | |
const bucketKey = req.params.bucketKey | |
const objectKey = file.originalname | |
const socketId = req.body.socketId | |
const nodeId = req.body.nodeId | |
const opts = { | |
chunkSize: 5 * 1024 * 1024, //5MB chunks | |
concurrentUploads: 3, | |
onProgress: (info) => { | |
if (socketId) { | |
const socketSvc = ServiceManager.getService( | |
'SocketSvc') | |
const msg = Object.assign({}, info, { | |
bucketKey, | |
objectKey, | |
nodeId | |
}) | |
socketSvc.broadcast ( | |
'upload.progress', msg, socketId) | |
} | |
}, | |
onComplete: () => { | |
if (socketId) { | |
const socketSvc = ServiceManager.getService( | |
'SocketSvc') | |
const msg = { | |
bucketKey, | |
objectKey, | |
nodeId | |
} | |
socketSvc.broadcast ( | |
'upload.complete', msg, socketId) | |
} | |
}, | |
onError: (error) => { | |
if (socketId) { | |
const socketSvc = ServiceManager.getService( | |
'SocketSvc') | |
socketSvc.broadcast ( | |
'upload.error', error, socketId) | |
} | |
} | |
} | |
const response = | |
await ossSvc.uploadObjectChunked ( | |
() => forgeSvc.get2LeggedToken(), | |
bucketKey, | |
objectKey, | |
file, opts) | |
res.json(response) | |
} catch (error) { | |
res.status(error.statusCode || 500) | |
res.json(error) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment