Mike U
Mike U

Reputation: 3061

Is it possible to List Azure Blobs Where Last Modified Date > Some Date

Is it possible to List all blobs on a container where Last Modified Date is greater than a specified date.

I have a container with millions of blobs and want to copy those blobs to a backup container, however don't want to loop through all blobs checking each for Last Modified Date.

Upvotes: 9

Views: 32349

Answers (5)

nothrow
nothrow

Reputation: 16178

It should be possible with API 2021-04-10 and later. There is call to search by tag, and querying following returns all this years blobs (UrlEncoded LastModified > '2023-01-01 00:00:00Z'):

https://myaccount.blob.core.windows.net/mycontainer?restype=container&comp=blobs&where=LastModified+%3e+%272023-01-01+00%3a00%3a00Z%27

Upvotes: 3

candlebar
candlebar

Reputation: 81

This is how I did it in Java. It prints the blobs modified in the last day. I am sure the C# equivalent is pretty similar:


BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().endpoint("https://youraccount.blob.core.windows.net").sasToken("yourtoken").buildClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("yourcontainer");

BlobListDetails details = new BlobListDetails().setRetrieveMetadata(true);
ListBlobsOptions options = new ListBlobsOptions().setPrefix("yourprefix").setDetails(details);

blobContainerClient.listBlobs(options, null).stream().filter(blob ->  blob.getProperties().getLastModified().isAfter(OffsetDateTime.now().minusDays(1))).forEach(blob -> System.out.printf("Name: %s %s%n", blob.getName(), blob.getProperties().getLastModified()));

Upvotes: 3

huha
huha

Reputation: 4255

If you look into the REST API documentation there is not parameter which does filtering for date/time. So the only way to do this is either listing all blobs and afterwards filter by your criteria (which is also done by Mustafa Salmans answer), or organize blobs by date as Gaurav Mantri already wrote.

Upvotes: 0

Mustafa Salam
Mustafa Salam

Reputation: 150

It is possible to do using Powershell. Please see below snippet.

$StorageAccountName = "AccountName" 
$StorageAccountKey = "What_ever_your_key_is_123asdf5524523A=="
$Context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$ContainerName = "Container"

$Blobs = Get-AzureStorageBlob -Container $ContainerName -Context $Context `
| Where-Object{$_.LastModified.DateTime -gt (Get-Date).Date}

Above command will get the blobs for current day from midnight.

You can then use the functions on Get-Date cmdlet to further narrow down the timeframe as below.

$Blobs = Get-AzureStorageBlob -Container $ContainerName -Context $Context `
| Where-Object{$_.LastModified.DateTime -gt ((Get-Date).Date).AddDays(-1)}

You can also sort this by piping to Sort-Object cmdlet as below to sort on any property (I sorted on date in below example).

$Blobs = Get-AzureStorageBlob -Container $ContainerName -Context $Context `
| Where-Object{$_.LastModified.DateTime -gt (Get-Date).Date.AddDays(-1)} `
| Sort-Object -Property Date

Upvotes: 6

Gaurav Mantri
Gaurav Mantri

Reputation: 136296

Is it possible to List all blobs on a container where Last Modified Date is greater than a specified date.

As of today it is not possible to do so. Blob service does not provide querying capabilities. When you list the blobs, Blob service will return you a list sorted by blob's name.

Not now, but going forward if you need this capability you may want to organize blobs by dates by prefixing their names with year, month and date. Then you can ask blob service to return you blobs names of which start with a particular prefix. If you use Azure App Service, do take a look at how diagnostics data for an Azure App Service is stored in a blob container. It does prefixing by year, month and date.

Upvotes: 6

Related Questions