1

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?

3
  • Is it working locally ? If yes, Make sure you have added a relevant App setting for Storage in the Portal App settings blade. Commented Aug 21, 2019 at 4:52
  • yes, locally it is working correctly. Which settings do you mean? Commented Aug 21, 2019 at 5:57
  • ok I did it with context.bindings Commented Aug 21, 2019 at 9:59

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.