How to get files modified today from Azure Blob Storage

Viewed 4910

I want to get files from Azure Blob Storage that are modified everyday. We have a container in Azure which is filled by two Excel files every day, and I need to get those files.

So far, I'm only able to get one file using latestmodifiedon. How can I get both files?

private static DataSet GetExcelBlobData()
{
    var containerName = "salesbycontract";
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Microsoft.Azure.CloudConfigurationManager.GetSetting("StorageConnectionString"));

    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);
    CloudBlockBlob blockbob = container.ListBlobs().OfType<CloudBlockBlob>().OrderByDescending(m => m.Properties.LastModified).ToList().First();

    var x = blockbob.Name;
    Console.WriteLine(x);

    DataSet ds;
    using (var memstream = new MemoryStream())
    {
        blockbob.DownloadToStream(memstream);

        var excelReader = ExcelReaderFactory.CreateOpenXmlReader(memstream);
        ds = excelReader.AsDataSet();
        excelReader.Close();
    }
    return ds;
}
2 Answers

Just add a Where clauses and compare to DateTime.Today:

var blockbob = container.ListBlobs().OfType<CloudBlockBlob>()
    .Where(m => m.Properties.LastModified.Value.Date == DateTime.Today).ToList().First();

I added a working example to my GitHub repository that uses dotnet core with the latest WindowsAzure.Storage SDK:

public async Task RetrieveBlobsModifiedTodayAsync()
{
    var container = _blobClient.GetContainerReference(_storageAccount.ContainerName);

    BlobContinuationToken blobContinuationToken = null;
    do
    {
        var results = await container.ListBlobsSegmentedAsync(null, blobContinuationToken);

        var blobs = results.Results.OfType<CloudBlockBlob>()
            .Where(b => b.Properties.LastModified != null && b.Properties.LastModified.Value.Date == DateTime.Today);

        blobContinuationToken = results.ContinuationToken;
        foreach (var item in blobs)
        {
            Console.WriteLine(item.Uri);
        }
    } while (blobContinuationToken != null); // Loop while the continuation token is not null. 
}

Migrated from Fikri Hailal's (OP) November 8, 2018 edit that added an answer to their question:

Below is the query which work for my case:

var blockbob = container.ListBlobs().OfType<CloudBlockBlob>().OrderByDescending(m => m.Properties.LastModified).ToList().Take(2);
Related