Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions: How to read blob input for nodejs?

I have setup an azure function which is triggered from a blob being added to a specific container. The blob is a .zip file my intention is to use adm-zip to extract the blob to a directory and then read the contents. I am confused about the the documentation here: https://github.com/Azure/azure-content/blob/master/articles/azure-functions/functions-bindings-storage.md#blob-trigger-supported-types Which says the input parameter can be either an Object or String. I don't see any place in the function.json to specify what I want the input to be.

In my code below, it the type seems to be a string, but since it doesn't print anything I assume it's actually a buffer of bytes representing the file contents. In order to work with this, I tried to write the buffer to a local file, but it was not successful. It did not throw an error or print out saved blob to...

In my function.json I have this:

{
  "bindings": [
    {
      "name": "xmlZipBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "balancedataxml",
      "connection": "sc2iq_STORAGE"
    }
  ],
  "disabled": false
}

And then I have xmlZipBlob as the second argument to my function:

var fs = require('fs');

module.exports = function (context, xmlZipBlob) {
    context.log('Node.js blob trigger function processed blob:', xmlZipBlob);
    context.log(`typeof xmlZipBlob:`, typeof xmlZipBlob);

    fs.writeFile('xmlZip.zip', xmlZipBlob, (err) => {
        if (err) {
            throw err;
        }

        context.log('saved blob to loal file called xmlZip.zip');
        context.done();
    });
};

1. What is the type of the input paramater to the function for blobs?

2. How do I control whether the input parameter is an Object or String?

3. Is it possible to use the native node fs module to write to local file system for extracting .zip file?

Update: I believe this is failing due to attempting to use the file system and have opened separate more isolated question here: Azure Functions: Nodejs, What are restrictions / limitations when using file system?

4. Is there a better alternative than using .zip files?

Until that is solved, I think I will have come up with different solution for dealing with the .zip file. Either doing everything in memory with streams, or avoiding .zip files all together and just building a larger xml file out of the smaller xml files. Either way, it seems there should be some documentation or warning on what it would take to port a node function that depends on the file system to an azure functions.

like image 620
Matt Mazzola Avatar asked Aug 24 '16 04:08

Matt Mazzola


1 Answers

In Node functions (as opposed to C# where you declare the parameter type in your function definition) the input type defaults to string. If you're trying to bind to binary data, you can indicate that in your function.json binding via the dataType property, e.g.:

{
  "bindings": [
    {
      "name": "xmlZipBlob",
      "type": "blobTrigger",
      "dataType": "binary",
      "direction": "in",
      "path": "balancedataxml",
      "connection": "sc2iq_STORAGE"
    }
  ]
}

This will pass the blob input to your function as a Node Buffer. Note that this is not streaming - the buffer is read entirely into memory.

Regarding your question on file system access, I answered that question on your other SO post :)

like image 185
mathewc Avatar answered Oct 18 '22 20:10

mathewc