8

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.

Mike U
  • 2,901
  • 10
  • 32
  • 44
  • See [here](https://stackoverflow.com/questions/14440506/how-to-query-cloud-blobs-on-windows-azure-storage/36014920#36014920) as well – Matt Jun 27 '17 at 10:31

5 Answers5

6

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.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
6

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
Mustafa Salam
  • 150
  • 1
  • 2
  • 12
  • It's best to store `Get-AzureStorageBlob -Container $ContainerName -Context $Context` in its own variable, and then use that variable to pipe to `Where-Object{$_.LastModified.DateTime -gt (Get-Date).Date}`. Otherwise you'll be kicking yourself if the call to `Get-AzureStorageBlob` takes ages to run and you find out you used the wrong date. – David Klempfner Apr 07 '21 at 03:16
3

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()));
candlebar
  • 81
  • 3
3

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

nothrow
  • 15,882
  • 9
  • 57
  • 104
0

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.

huha
  • 4,053
  • 2
  • 29
  • 47