Integrate Node.js Azure Functions With BLOB Storage

Most of us merely use Azure Functions to just execute a bunch of lines of code without being aware of its integration capability with other Azure PaaS services. You can integrate either input or output operations of you Azure Functions with other Azure services. In the below example, you can see how an output can be written to a BLOB service after Azure Functions execution.
 
I have the following Azure function written in node.js, i.e., JavaScript-based. It will calculate and print the square of a number based on a parameter "number".
  1. module.exports = async function (context, req) {  
  2.     context.log('JavaScript HTTP trigger function processed a request.');  
  3.   
  4.     if (req.query.number || (req.body && req.body.number)) {  
  5.         context.res = {  
  6.             // status: 200, /* Defaults to 200 */  
  7.             body: "The square is " + (req.query.number * req.query.number  || req.body.number * req.body.number)  
  8.         };  
  9.       
  10.     }  
  11.     else {  
  12.         context.res = {  
  13.             status: 400,  
  14.             body: "Please pass a number on the query string or in the request body"  
  15.         };  
  16.     }  
  17. }; 
The above Azure function is working as expected and it's printing the output too. But I need this output to be written in BLOB storage too.
So, you need to select the "Integrate" configuration tab of your Azure function as mentioned in the below image. Also, notice that on the right side of the tab, you have options for both "Input" and "Output". If you want to read some parameter values from BLOB, use "Input" and if you want to write something to BLOB after function execution, use "Output". This article only covers "Output" and even though I am only talking about BLOB service, you can use other Azure services too for this integration purpose.
 
Integrate Node.js Azure Functions With BLOB Storage
 
I selected the "Outputs" for integration and you can see all supported Azure integration services as like below image. I opted for BLOB among these services.
 
Integrate Node.js Azure Functions With BLOB Storage
 
Once you selected an integration service, you must install supporting extensions as the below image shows. You can do this installation inside the Azure portal itself and there is no need to go out of it.
 
Integrate Node.js Azure Functions With BLOB Storage
 
After this extension installation, you see a window like the below image. Please notice a new output parameter generated with a name "outputBlob", which is a reference to a BLOB service. The "Path" section is the reference to a sub-path under the storage account path, to which this specific BLOB would be written. Just below that section, you will notice a configured BLOB service under section "Storage account connection". You have an option to configure new separate BLOB storage too if needed.
 
Integrate Node.js Azure Functions With BLOB Storage
 
After clicking Save, you will see this BLOB under the "Outputs" section of your "Integrate" tab.
 
Integrate Node.js Azure Functions With BLOB Storage
 
You need to add a line to your function code as below to write the outputs to the BLOB. The additional line is self-explanatory as we are just writing the output to the new parameter "outputBlob", which has a reference to the BLOB. 
  1. module.exports = async function (context, req) {  
  2.     context.log('JavaScript HTTP trigger function processed a request.');  
  3.   
  4.     if (req.query.number || (req.body && req.body.number)) {  
  5.         context.res = {  
  6.             // status: 200, /* Defaults to 200 */  
  7.             body: "The square is " + (req.query.number * req.query.number  || req.body.number * req.body.number)  
  8.         };  
  9.         context.bindings.outputBlob = "The square is " + (req.query.number * req.query.number  || req.body.number * req.body.number);  
  10.     }  
  11.     else {  
  12.         context.res = {  
  13.             status: 400,  
  14.             body: "Please pass a number on the query string or in the request body"  
  15.         };  
  16.     }  
  17. };  
You know the testing pane for Azure function to test our code. Please notice the parameter I set under body section. You also see that the output result is as expected. Now, we need to check the BLOB too.
 
Integrate Node.js Azure Functions With BLOB Storage
Integrate Node.js Azure Functions With BLOB Storage
 
I opened the specific storage account and it is BLOB that we configured as integrated service to Azure function. You can see that the expected output has been written into the BLOB.
 
Integrate Node.js Azure Functions With BLOB Storage 
 
Integrate Node.js Azure Functions With BLOB Storage
 
I made this tutorial entirely inside the Azure portal; so, no source code is available. But as a continuation of this "Outputs" integration, I am recommending to proceed with integration for "Inputs" too, where you read the BLOB for some values.


Similar Articles