Getting the Latest File Modified from Azure Blob

Getting the latest file modified from Azure Blob

Each IListBlobItem is going to be a CloudBlockBlob, a CloudPageBlob, or a CloudBlobDirectory.

After casting to block or page blob, or their shared base class CloudBlob (preferably by using the as keyword and checking for null), you can access the modified date via blockBlob.Properties.LastModified.

Note that your implementation will do an O(n) scan over all blobs in the container, which can take a while if there are hundreds of thousands of files. There's currently no way of doing a more efficient query of blob storage though, (unless you abuse the file naming and encode the date in such a way that newer dates alphabetically come first). Realistically if you need better query performance I'd recommend keeping a database table handy that represents all the file listings as rows, with things like an indexed DateModified column to search by and a column with the blob path for easy access to the file.

How to retrieve latest file from SharePoint to blob using logic app?

Follow the workaround

You can use the below Trigger & Connector

  • Sharepoint (Trigger) - When a file is created or modified in a folder

You can use this connector to select and get the exact Directory to fetch the recent Modified/Created File.

  • Azure Blob Storage (Connector)- Create Blob (V2)
    Use this connector to create a blob.

Sample Image

Result

Sample Image
Sample Image

Modified file fetched and added in a Blob

Sample Image

Refer here for more information

Updated Answer

Here is the list of available directories in SharePoint that will be shown in a SharePoint Trigger. You can select according to your requirement.

Sample Image

Get last modified date of latest file in a storage account

Finally got the answer(simply last modified date of all files,not the specific latest file).

//Code to get the last modified date

CloudBlockBlob blockBlob = Container.GetBlockBlobReference(blobName);
blockBlob.FetchAttributes();
var lastModifiedDate = blockBlob.Properties.LastModified;
Console.WriteLine(lastModifiedDate);

How to get files modified today from Azure Blob Storage

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.
}


Related Topics



Leave a reply



Submit