Nafis Islam
Nafis Islam

Reputation: 1509

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?

Upvotes: 5

Views: 10423

Answers (1)

Peter Bons
Peter Bons

Reputation: 29711

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")
    });

Upvotes: 8

Related Questions