0

I'm trying to read the content of a blob inside an azure function.

Here's the code:

Note: If I comment out the using block and return the blob i.e.

return new OkObjectResult(blob);

I get back the blob object.

However, if I use the using block, I get 500.

Any idea why I can't get the content?

string storageConnectionString = "myConnectionString";
CloudStorageAccount storageAccount;
CloudStorageAccount.TryParse(storageConnectionString, out storageAccount);
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = cloudBlobClient.GetContainerReference("drawcontainer");


var blob = drawingsContainer.GetBlockBlobReference("notes.txt");

using (StreamReader reader = new StreamReader(blob.OpenRead()))
{
    content = reader.ReadToEnd();
}
return new OkObjectResult(content);

2 Answers 2

1

HTTP 500 indicates that the code has error. The most probable reason for error is the variable 'content'. Define the variable 'content' outside the using block as the scope of the content variable defined inside it is limited to the block only. Declare it outside the using block, something like below:

    try
    {
        string content = string.Empty;
        using (StreamReader reader = new StreamReader(blob.OpenRead()))
        {
            content = reader.ReadToEnd();
        }
    }
    catch (Exception ex)
    {
        // Log exception to get the details.    
    }

Always make use of try catch to get more details about errors in the code.

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

4 Comments

Thanks for the answer. the variable content is not the issue (even though its declaration was missing). I placed try catch but didn't get a descriptive error
I placed some logs and I think the issue is the blob.OpenRead() - I think the OpenRead might not exists on the blob but I still didn't resolve the issue
The error that keep repeating is: error CS0103: The name 'CloudStorageAccount' does not exist in the current context
@Avi Would you please provide the nuget package and the framework details? Also, please add/edit the complete code snippet.
1

The OpenRead method didn't exist so I used the async one and it solved it.

I got to this solution after creating an azure function in VS and publishing it and it works.

Here's the code I used:

 public static class Function1
{
    [FunctionName("Function1")]
    public static async Task<ActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
    {
        log.Info("C# HTTP trigger function processed a request.");
        string storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=avitest19a1c;AccountKey=<AccessKey>";
        CloudStorageAccount storageAccount = null;
        CloudStorageAccount.TryParse(storageConnectionString, out storageAccount);
        CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer drawingsContainer = cloudBlobClient.GetContainerReference("drawcontainer");

        var blob = drawingsContainer.GetBlockBlobReference("notes.txt");

        string content = string.Empty;
        **var contentStream = await blob.OpenReadAsync();**
        using (StreamReader reader = new StreamReader(contentStream))
        {
            content = reader.ReadToEnd();
        }

        return new OkObjectResult(content);

    }
}

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.