Skip to content

Instantly share code, notes, and snippets.

@panreel
Last active November 6, 2022 09:29
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panreel/b94c8c4e05ff34f0c72e to your computer and use it in GitHub Desktop.
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]
//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