1
votes

I'm totally new in azure and I would like to create azure function, which will read the content from azure storage container file.json.

Folder structure : Storage account name: storageaccounttest Container name: test File name: file.json

File.json:

[

    {
        "name":"Kate",
        "age":"28"
    },
    {
        "name":"John",
        "age":"30"
    }
]

Cors on storage account: get enabled.

Environemnts variable added: process.env.AZURE_STORAGE_NAME and process.env.AZURE_STORAGE_KEY and process.env.AZURE_CONNECTION_STRING

I'm using VisualStudioCode to deploy the function. I installed locally the dependencies: "dependencies": { "azure-storage": "^2.10.3", "dotenv": "^8.1.0" }

I choose the javascript -> HttpTrigger fn-> anonymus options

I'm using getBlobToText fn. My index.js:

var storage = require('azure-storage');
var blobService = storage.createBlobService();
var containerName = 'test';
var blobName = 'file.json';

module.exports =  blobService.getBlobToText(
    containerName,
    blobName,
    function(err, blobContent) {
        if (err) {
            console.error("Couldn't download blob");
            console.error(err);
        } else {
            console.log("Sucessfully downloaded blob");
            console.log(blobContent);
        }
    });

Fn is deployed successfully, but I'm not able to see results. After start, fn is finished with status 500, Internal Server Errror, Console: No new trace in the past 1 min(s).

What I made wrong?

1
Is it working locally ? If yes, Make sure you have added a relevant App setting for Storage in the Portal App settings blade.Ketan
yes, locally it is working correctly. Which settings do you mean?kah
ok I did it with context.bindingskah

1 Answers

2
votes

Just summarized for helping others who get the same issue.

I think you were using context.binding.response to pass the blobContent value to the output response as the offical document Azure Functions JavaScript developer guide said.

Here is my sample code with Promise feature to solve it.

var azure = require('azure-storage');
var blobService = azure.createBlobService();
var containerName = 'test';
var blobName = 'file.json';

async function getBlobContent(containerName, blobName) {
  return new Promise((resolve, reject) => {
    blobService.getBlobToText(containerName, blobName, function(err, blobContent) {
        if (err) {
            reject(err);
        } else {
            resolve(blobContent);
        }
    });
  });
}

module.exports = async function (context, req) {
    await getBlobContent(containerName, blobName).then(
        function(content) {
            context.res = {
                headers: {"Content-Type": "application/json"},
                body: content
            }
        }, function(error) {
            context.res = {
                status: 400,
                body: error
            }
        }
    );
};

It works as the figure below.

enter image description here