How to query Cloud Blobs on Windows Azure Storage

后端 未结 5 657
青春惊慌失措
青春惊慌失措 2020-12-10 00:43

I am using Microsoft.WindowsAzure.StorageClient to manipulate blobs on Azure storage. I have come to the point where the user needs to list the uploaded files and modify/del

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 01:20

    The method ListBlobs retrieves the blobs in that container lazily. So you can write queries against that method that are not executed until you loop (or materialize objects with ToList or some other method) the list.

    Things will get clearer with few examples. For those that don't know how to obtain a reference to a container in your Azure Storage Account, I recommend this tutorial.

    Order by last modified date and take page number 2 (10 blobs per page):

    blobContainer.ListBlobs().OfType()
             .OrderByDescending(b=>b.Properties.LastModified).Skip(10).Take(10);
    

    Get specific type of files. This will work if you have set ContentType at the time of upload (which I strongly recomend you do):

    blobContainer.ListBlobs().OfType()
             .Where(b=>b.Properties.ContentType.StartsWith("image"));
    

    Get .jpg files and order them by file size, assuming you set file names with their extensions:

    blobContainer.ListBlobs().OfType()
        .Where(b=>b.Name.EndsWith(".jpg")).OrderByDescending(b=>b.Properties.Length);
    

    At last, the query will not be executed until you tell it to:

    var blobs = blobContainer.ListBlobs().OfType()
                              .Where(b=>b.Properties.ContentType.StartsWith("image"));
    
    foreach(var b in blobs) //This line will call the service, 
                            //execute the query against it and 
                            //return the desired files
    {
       // do something with each file. Variable b is of type CloudBlob
    }
    

提交回复
热议问题