5

Could anyone let me know how to read a text file from Azure Blob Storage?

Kris van der Mast
  • 16,343
  • 8
  • 39
  • 61
SRA
  • 1,681
  • 7
  • 27
  • 49

2 Answers2

15

It's pretty easy:

string text = CloudStorageAccount.Parse("<your connection string>").CreateCloudBlobClient().GetBlobReference("path/to/the/blob.txt").DownloadText();

Of course, if the blob is in a public container, you can just do:

string text = new WebClient().DownloadString("http://youraccount.blob.core.windows.net/path/to/blob.txt");
user94559
  • 59,196
  • 6
  • 103
  • 103
  • I do not have access to `GetBlobReference("String")` instead I have `GetBlobReferenceFromServer()` am I forgetting something? – jdave Apr 21 '16 at 16:29
  • This answer is from 2010. It wouldn't surprised me if the method names have changed in the intervening years. – user94559 Apr 21 '16 at 16:55
2
// connect to development storage. To connect to azure storage use connection string
CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
CloudBlobClient client = storageAccount.CreateCloudBlobClient();

// if you know the blob you want to access you can do this:
CloudBlob blob = client.GetBlobReference("containername/blobname.txt");

// To display text in console:
Console.WriteLine(blob.DownloadText());
Console.ReadKey();
crunchy
  • 705
  • 1
  • 13
  • 35