将JSON对象存储到Azure Blob存储

时间:2018-11-15 16:55:49

标签: node.js azure azure-storage azure-storage-blobs

有什么方法可以存储json对象而不将其转换为流?将其转换为流后即可上传。但是,有什么东西可以将对象存储为{something} .json而无需将其转换为流吗?

我现在要做什么

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

有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

当然,您可以像这样使用createblockblobfromtext发送文本:

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