Perform the following steps:
- Create a class named StorageManager and paste in the following code. This code connects to the specified storage account, reads the data from the blobs, and returns a Stream object to the caller function:
class StorageManager
{
public async Task<Stream> ReadBlob(string BlobName)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(configuration.GetConnectionString("StorageConnection"));
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer ExcelBlobContainer = cloudBlobClient.GetContainerReference("Excel");
CloudBlockBlob cloudBlockBlob = ExcelBlobContainer.GetBlockBlobReference(BlobName);
return await cloudBlockBlob.OpenReadAsync();
}
}
- Paste the following namespace references into the StorageManager class:
using Microsoft.Extensions.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
using System.Threading.Tasks;
- Finally, add a connection string (if it's not been done already) of the storage account to the local.settings.json file, as shown here: