Logo
Published on

A Guide to Storing JSON Object Data in Azure Blob Storage

Authors
  • Name
    Twitter

Photo by Glenn Carstens-Peters on Unsplash

Introduction: In the world of cloud computing, Azure Blob Storage offers a dependable and expandable option for storing several kinds of data, including JSON objects. In order to store and retrieve JSON data, Azure Blob Storage provides a flexible and secure platform. JSON (JavaScript Object Notation) is a popular data transfer standard. By utilising Azure Blob Storage’s features and APIs, we will examine how to store JSON object data there in this blog article and develop reliable data storage solutions:1.

  1. **Set up Azure Blob Storage:
    • Create an Azure Storage account first:** Use the Azure interface to create an Azure Storage account. Depending on your needs, select the right storage account type and setting options.
    • Construct a Blob Container: To hold your JSON data, construct a blob container inside the storage account. Set up the container’s access control, retention, and other settings as necessary.
  2. **Access Azure Blob Storage from Node.js:
    • Installing the Azure Blob Storage SDK:** Utilise the client library provided by the @azure/storage-blob package in Node.js to communicate with Azure Blob Storage.
    • Set up authentication: To use the Azure Blob Storage account, authenticate your Node.js application. Depending on your security needs, you can employ connection strings, shared access signatures (SAS), or Azure Active Directory (AD) authentication methods.
  3. **Store JSON Object Data in Azure Blob Storage:
    • Serialize JSON Object:** Create a string from your JSON object by utilising the JSON serialisation method.For storing, use the stringify() function.
    • Create a Blob Client: To create a blob client, use the Azure Blob Storage SDK’s BlobServiceClient and enter the connection information and container name.
    • Upload JSON Data: Create a new blob using the blob client, and then upload the serialised JSON data for the blob’s content. To improve the organisation and retrieval of your JSON objects, set the relevant information and properties.
      Note: Code is also added in the end.
  4. **Retrieve JSON Object Data from Azure Blob Storage:
    • Access Blob Client:** Create a blob client and enter the name and container information for the blob.
    • Download Blob Content: To download the blob’s content as a buffer or stream, use the blob client.- Deserialize JSON Data: You can manipulate and use the data in your application by using JSON.parse() to convert the downloaded material back into a JSON object.
      Note: Code is also added in the end.
  5. **Additional Considerations:
    • Handling Large JSON Data:** To improve storage and retrieval performance for huge JSON data, think about chunking the data into smaller chunks or compressing it.
    • Metadata and Indexing: Utilise the metadata feature of Azure Blob Storage to give your JSON objects additional specific characteristics and indexing data, enabling search and query operations.
    • Data Security and Encryption: By using encryption options offered by Azure Blob Storage, such as server-side encryption or client-side encryption, you may ensure the security of your JSON data.
// server.js
const express = require('express')
const cors = require('cors')
const routes = require('./routes/index')
const app = express()
const port = process.env.PORT || 8000
app.use(cors({ credentials: true }))
app.use(express.json())
app.use('/', routes)
app.listen(port, () => {
  console.log(`Server runing on port ${port}`)
})

// router/index.js
const express = require('express')
const routes = express.Router()
const blobStorageController = require('../controller/index')
routes.post('/save/blobStorage', blobStorageController.saveBlobStorage)
routes.get('/fetch/blobStorage/:blobname', blobStorageController.fetchBlobStorage)
module.exports = routes

// controller/index.js
const { v4: uuidv4 } = require('uuid')
const { BlobServiceClient } = require('@azure/storage-blob')
const connectionString = 'Your_connection_string' /* Need to replace with your connection string */
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString)
const containerName = 'Your_container_name' /* Need to replace with your container name */

/* Business Logic for storing data inside Blob Storage */
exports.saveBlobStorage = async function (req, res) {
  const blobName = `blobStorage_${uuidv4()}.json`
  let { name, age, email } = req.body
  const jsonData = {
    name: name,
    age: age,
    email: email,
  }

  const jsonString = JSON.stringify(jsonData)
  try {
    const containerClient = blobServiceClient.getContainerClient(containerName)
    // To check container is exist or not. If not exist then create it.
    await containerClient.createIfNotExists()
    // Get a block blob client pointing to the blob
    const blockBlobClient = containerClient.getBlockBlobClient(blobName)
    await blockBlobClient.upload(jsonString, Buffer.byteLength(jsonString))

    res.json({
      message: 'JSON data stored successfully',
      blob_storage_name: blobName,
    })
  } catch (error) {
    res.json({
      message: 'Error uploading JSON data',
    })
  }
}

/* Business Logic for fetching data from Blob Storage */
exports.fetchBlobStorage = async function (req, res) {
  let blobName = req.params.blobname
  try {
    const containerClient = blobServiceClient.getContainerClient(containerName)
    // Get a block blob client pointing to the blob
    const blockBlobClient = containerClient.getBlockBlobClient(blobName)
    // Download the blob data as a buffer
    const blobData = await blockBlobClient.downloadToBuffer()
    // Convert the buffer to a string (assuming JSON data)
    const jsonData = blobData.toString()
    // Parse the JSON data
    const parsedData = JSON.parse(jsonData)

    res.json({
      message: 'JSON data fetched successfully',
      data: parsedData,
    })
  } catch (error) {
    res.json({
      message: 'Error fetching data from Azure Blob Storage',
    })
  }
}

//Note: Don't consider this code is optimized. This code is just for your understanding.

Conclusion: A robust and scalable option for storing JSON object data in the cloud is Azure Blob Storage. You can effectively store and retrieve JSON data by using Azure Blob Storage’s capabilities and APIs by following the instructions provided in this article. Azure Blob Storage offers a dependable and adaptable storage platform for your JSON object data requirements, whether you are developing data-intensive apps, implementing data backups, or storing logs and telemetry.