0

I am trying to access content from the Blob Storage from an Azure function. I have the following:

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{   
    string path = req.Query["path"];   

    string storageConnectionString = "...";
    CloudStorageAccount blobAccount = CloudStorageAccount.Parse(storageConnectionString);
    CloudBlobClient blobClient = blobAccount.CreateCloudBlobClient();
    CloudBlobContainer blobContainer = blobClient.GetContainerReference("content");
    CloudBlockBlob cloudBlockBlob = blobContainer.GetBlockBlobReference(path);*

    return new HttpResponseMessage(HttpStatusCode.OK) {
        Content = /* to do - content of blob */
    };    
}

However, I am struggling to tell the function to recognize Microsoft.WindowsAzure.Storage namespace:

2019-11-07T09:59:57.729 [Error] run.csx(7,17): error CS0234: The type or namespace name 'WindowsAzure' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

I feel I am missing something major in my understanding of Azure Functions, as it should not be such a challenge to pull in namespaces/packages to work with Azure from within an Azure function.

Thanks a lot!

kyuz0
  • 101
  • 4

1 Answers1

0

In c# script Function, you don't need to init blob client to read blob. Azure function provides the Blob binding to read/write blob, check this doc:Blob Input - example.

Firstly go to your function Integrate, set it like the below pic. With this just bind the inputBlob to stream to read content or bind to CloudBlockBlob type and just use the CloudBlockBlob method.

And the path supports bind to container, and in the function just bind to CloudBlobContainer type.

enter image description here

The below is my test code to read a text file.

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.IO;
using System.Collections.Generic;

public static void Run(HttpRequest req, Stream inputBlob,ILogger log)
{
    StreamReader reader = new StreamReader(inputBlob);
    string  oldContent = reader.ReadToEnd();
    log.LogInformation($"oldContent:{oldContent}");  
}

enter image description here

Hope this could help you, if you still have other problem please feel free to let me know.

Community
  • 1
  • 1
George Chen
  • 13,703
  • 2
  • 11
  • 26