How to append JSON blob in nodeJS-AzureStorage-Blob?

Paty Hernandez 21 Reputation points
2024-08-13T19:50:21.67+00:00

Hi

I have a lambda function in node 20. I am using the azure storage blob library. I need to add content to an JSON append blob in azure storage.

appendBlobClient.appendBlock(json, json.length).then(
			response => {
				resolve("Upload success");
			},
			error => {
				reject(error);
			}
		);

But since this is a JSON, the content is added as if it were a new JSON[]. And what I need is for the content to be added INSIDE THE JSON that already exists in azure storage. Not as a new JSON

Any ideas?

Azure Storage Explorer
Azure Storage Explorer
An Azure tool that is used to manage cloud storage resources on Windows, macOS, and Linux.
252 questions
0 comments No comments
{count} votes

Accepted answer
  1. Nehruji R 7,306 Reputation points Microsoft Vendor
    2024-08-14T13:27:13.6933333+00:00

    Hello Paty Hernandez,

    Greetings! Welcome to Microsoft Q&A Storage.

    I understand that you’re trying to append JSON content to an existing JSON object in an Azure Storage Append Blob, but the content is being added as a new JSON array instead of merging with the existing JSON object. To achieve this, you’ll need to read the existing JSON content from the blob, modify it, and then write it back.

    Steps to perform the same,

    • Read the existing JSON content from the blob.
    • Parse the JSON content and modify it.
    • Write the updated JSON content back to the blob.
    const { BlobServiceClient } = require('@azure/storage-blob');
    const appendBlobClient = BlobServiceClient.fromConnectionString('your_connection_string').getContainerClient('your_container_name').getAppendBlobClient('your_blob_name');
    
    async function appendJsonToBlob(newJson) {
        try {
            // Step 1: Read the existing JSON content from the blob
            const downloadBlockBlobResponse = await appendBlobClient.download(0);
            const downloadedContent = await streamToString(downloadBlockBlobResponse.readableStreamBody);
            let existingJson = JSON.parse(downloadedContent);
    
            // Step 2: Modify the existing JSON content
            existingJson = { ...existingJson, ...newJson };
    
            // Step 3: Write the updated JSON content back to the blob
            const updatedContent = JSON.stringify(existingJson);
            await appendBlobClient.appendBlock(updatedContent, updatedContent.length);
    
            console.log("Upload success");
        } catch (error) {
            console.error("Error:", error);
        }
    }
    
    // Helper function to convert stream to string
    async function streamToString(readableStream) {
        return new Promise((resolve, reject) => {
            const chunks = [];
            readableStream.on("data", (data) => {
                chunks.push(data.toString());
            });
            readableStream.on("end", () => {
                resolve(chunks.join(""));
            });
            readableStream.on("error", reject);
        });
    }
    
    // Example usage
    const newJson = { key: "value" };
    appendJsonToBlob(newJson);
    
    

    Note: The above code is an example code, this code reads the existing JSON content from the blob, merges it with the new JSON content, and writes the updated JSON back to the blob. Make sure to replace 'your_connection_string', 'your_container_name', and 'your_blob_name' with your actual Azure Storage details.

    reference - https://github.com/jcpengy10/campaignion-app/blob/b7a54afd193728971fd59021c7f2b40289069990/node_modules/%40azure/storage-blob/README.md

    Hope the above information helps! please let us know if you have any further queries. I’m happy to assist you further.

    Please "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Paty Hernandez 21 Reputation points
    2024-08-15T00:23:57.3466667+00:00

    Thanks, I solved it by specifying the type of data that append did. Later by eliminating '][' from my application code.

    blockBlobClient.upload(newJson, newJson.length, { contentSettings: { contentType: 'application/json' } }
    ).then(
    			  response => {
    				resolve("Success operation");
    			  },
    			  error => {
    				reject(error);
    			  }
    			);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.