Last active
November 6, 2022 09:29
-
-
Save panreel/b94c8c4e05ff34f0c72e to your computer and use it in GitHub Desktop.
Upload a binary data from request body to Azure BLOB storage in Node.js [restify]
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
//NOTE: Do not use restify.bodyParser() - it blocks piping | |
//vars | |
var azure = require('azure-storage'); | |
// ... BLOB connection skipped ... | |
//Option 1 - Piping req stream to BLOB write stream (elegant) | |
function (req, res, next) { | |
//create write stream for blob | |
var stream = azure.createWriteStreamToBlockBlob( | |
containerID, | |
blobID); | |
//pipe req to Azure BLOB write stream | |
req.pipe(stream); | |
req.on('error', function (error) { | |
//KO - handle piping errors | |
}); | |
req.once('end', function () { | |
//OK | |
}); | |
} | |
//Option 2 - Using req as a stream (it is actually a Readable stream) | |
function (req, res, next) { | |
azure.createBlockBlobFromStream( | |
containerID, | |
BlobID, | |
req, | |
req.contentLength(), | |
function (error, result, response) { | |
//handle result | |
}); | |
} | |
//Option 3 - flush the req content to a Buffer and pass it like a binary string | |
function (req, res, next) { | |
azure.createBlockBlobFromText( | |
ContainerID, | |
BlobID, | |
req.read(), //convert req content to a Buffer | |
function (error, result, response) { | |
//handle result | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment