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.

UPDATE (2022) It appears that Microsoft now offers customizable Blob Index Tags. This should allow for adding a custom DateModified property or similar on blob metadata, and performing efficient “greater than” / “less than” queries against your blobs without the need for a separate database. (NOTE: It apparently only supports string values, so for date values you would need to make sure to save them as a lexicographically-sortable format like “yyyy-MM-dd”.)

Leave a Comment