Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get files modified today from Azure Blob Storage

Tags:

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;
}
like image 311
Fikri Hailal Avatar asked Nov 02 '18 07:11

Fikri Hailal


People also ask

How do I access files from blob storage?

Open a blob on your local computer Select the blob you wish to open. On the main pane's toolbar, select Open. The blob will be downloaded and opened using the application associated with the blob's underlying file type.

Would it be possible to modify an existing blob snapshot?

After you create a snapshot, you can read, copy, or delete it, but you cannot modify it.


1 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. 
}
like image 138
Martin Brandl Avatar answered Oct 20 '22 00:10

Martin Brandl