Search code examples
node.jsazureazure-storageazure-blob-storage

Store json object to Azure blob storage


is there any way to store a json object without converting it to stream? I can upload after converting it to stream. But is there any to to store object as {something}.json with out converting it to stream?

what I do now

const azureStorage = require('azure-storage');
const blobService = azureStorage.createBlobService(accountName, accountKey);
var Readable = require('stream').Readable
var msg = {
   a: "something",
   b: "anotherthing"
}
var stream = new Readable
stream.push(JSON.stringify(msg))
stream.push(null)
var option = {
   contentSettings: {contentType: 'application/json'}
}
stream.pipe(blobService.createWriteStreamToBlockBlob('container', 'something.json', option, function onResponse(error, result) {
  console.log("done")
}));

Is there a better way?


Solution

  • Sure, you can just send text using createblockblobfromtext like this:

    blobService.createBlockBlobFromText(
        'container', 
        'something.json',
        JSON.stringify(msg) 
        option, 
        function onResponse(error, result) {
            console.log("done")
        });