Get the Latest Modified File Azure Blob Storage using .NET
Today in this article, we shall see how to get latest modified File Azure Blob Storage using .NET .
We will use Storage blob Client library v12 for .NET.
We will also see how to get details on the date when the azure blob got created or deleted or leased etc.
Azure Blob storage is used for storing the data in the cloud.
Blob storage is optimized for storing unstructured data supporting multiple formats of files such as text or binary data.
Today in this article, we will cover below aspects,
Let’s see step by step all the above operation and understand how it works.
Prerequisites
- Create Azure subscription
- Create Storage Account in Azure Portal
Getting Started
Create any .NET application. Here I am using the Console .NET Core application,
Please install Nuget packages,
PM> Install-Package Azure.Storage.Blobs -Version 12.6.0
Or Using Nuget Package Manager
Please note that the Blob class provides a few properties related to blob time-specific attributes
Example
Get the last modified date for The Blob
Property name : LastModified
This gives you Blob’s last modified dates
Getting the date of the blob got created
Property name : CreatedOn
This property gives you a Blob Created date
Blob Deleted date
This property gives Blob Deleted date
Property name: DeletedOn
Blob LeaseDuration, LeaseState
Property name: LeaseDuration
This property gives Blob lease duration
We can get a list of all blobs for the given search criteria using the below one-liner,
var resultBlob = container.GetBlobs().OrderByDescending(m => m.Properties.LastModified).ToList();
Here below is the complete code base.
I have a total of 3 files in the storage account created at different timestamps.
string stAccountconnectionString = configuration["StorageAccountConString"];
string blobName = configuration["BlobName"];
string containerName = configuration["ContainerName"];
// Get a reference to a container
BlobContainerClient container = new BlobContainerClient(stAccountconnectionString, containerName);
var resultBlob = container.GetBlobs().OrderByDescending(m => m.Properties.LastModified).ToList();
foreach (BlobItem blobSt in resultBlob)
{
Console.WriteLine(blobSt.Name);
//Perform your business logic here
}
I get the list of all blobs based on last modified dates(descending order) as below,
You can use the below code and use additional properties as discussed above depending upon the requirements you are dealing with.
blobSt.Properties.DeletedOn blobSt.Properties.LastModified blobSt.Properties.CreatedOn
References:
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.
Hi,
I am getting error on m.Properties.LastModified.
Error: ‘char’ does not contain a definition for ‘Properties’ and no accessible extension method ‘Properties’ accepting a first argument of type ‘type’ could be found (are you missing a using directive or an assembly reference?).
NOTE: I installed the Azure Storage Blob from Nuget package.
Hello Vaish,
Thanks for your query. I hope you added required namespaces too?
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;