Tonyc
Tonyc

Reputation: 739

Read the blob content on Azure Storage

I'm using the Microsoft.Azure.Storage.Blob nuget package trying to get the list of the blobs in a container and than reading the content.

With the ListBlobs() method I see all the blobs. Every blob item has an URI but I cannot see the blob name that I need for the GetBlobReferenceFromServer().

For this reason the blob name is a constant in following sample code.

What is the right way? Do I have to split and parse the URI to find the blob name? Do I have to use another method?

Microsoft.Azure.Storage.Blob.CloudBlobContainer container = 
    new Microsoft.Azure.Storage.Blob.CloudBlobContainer(new Uri("https://myaccount.blob.core.windows.net/containername"), 
                                                        new Microsoft.Azure.Storage.Auth.StorageCredentials("myaccount", "**********=="));

IEnumerable<Microsoft.Azure.Storage.Blob.IListBlobItem> blobs = container.ListBlobs();

foreach (var blobItem in blobs)
{
    //string blobUri = blobItem.Uri.ToString();

    Microsoft.Azure.Storage.Blob.ICloudBlob blockBlob = container.GetBlobReferenceFromServer("blobname");
    MemoryStream downloadStream = new MemoryStream();
    blockBlob.DownloadToStream(downloadStream);

    string blobContent = Encoding.UTF8.GetString(downloadStream.ToArray());
}

Upvotes: 0

Views: 5077

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136366

With the ListBlobs() method I see all the blobs. Every blob item has an URI but I cannot see the blob name that I need for the GetBlobReferenceFromServer().

The reason for this is that ListBlobs method returns an enumerable of type IListBlobItem which does not have the name property. In order to get the name of the blob, you can cast it to either CloudBlob or CloudBlockBlob which implement this interface and you will be able to get the name of the blob which you can use GetBlobReferenceFromServer method.

BTW, once you have listed the blob you don't really need to call GetBlobReferenceFromServer method as you already have all the information about the blob as part of listing. GetBlobReferenceFromServer makes another request to storage to fetch same set of properties that you already have as part of listing.

So your code can simply be:

foreach (var blobItem in blobs)
{
    var blockBlob = (CloudBlockBlob) blobItem;
    MemoryStream downloadStream = new MemoryStream();
    blockBlob.DownloadToStream(downloadStream);

    string blobContent = Encoding.UTF8.GetString(downloadStream.ToArray());
}

Or, if you don't go down casting route, you can simply create an instance of CloudBlockBlob using the URI you got as part of the listing.

Something like:

foreach (var blobItem in blobs)
{
    var blockBlob = new CloudBlockBlob(blobItem.Uri, container.ServiceClient);
    MemoryStream downloadStream = new MemoryStream();
    blockBlob.DownloadToStream(downloadStream);

    string blobContent = Encoding.UTF8.GetString(downloadStream.ToArray());
}

Upvotes: 3

Related Questions