Reputation: 435
I want to programmatically list all blobs created in the Azure storage container after a certain time? Is there a way to list only those blob files?
Upvotes: 2
Views: 5551
Reputation: 29995
Currently, there is no direct way to list blobs after a certain time.
There're 2 workarounds here.
1.List all the blobs first -> fetch the creation time of each blob -> compare the creation time of the blob with the certain time. Like below:
myblobs = container_client.list_blobs()
#define a filter date or timestamp as per your need.
filter_date = "2021-01-18 or 2021-01-18 01:31:29"
for blob in myblobs:
#compare the creation time of blob with the filter_date
if str(blob.creation_time) > filter_date:
print(blob.name + ":" + str(blob.creation_time))
2.When create a blob, you can specify a prefix for your blob. The prefix can be defined as year-month-date(or you can add hour, minutes etc). Then when you list blobs by using list_blobs()
method, there is a parameter name_starts_with
. At this time, you can specify a date for name_starts_with
parameter.
Upvotes: 3
Reputation: 4301
If you like Powershell:
$daysAgo = 30
# this example uses OAuth token, you can change it to use a SAS token or account key
$ctx = New-AzStorageContext -StorageAccountName MyStorageAccount -UseConnectedAccount
# Get all blobs in container, get the Name and CreatedOn date
# then filter by days old and sort by descending date
Get-AzStorageBlob -Context $ctx -Container MyContainer | `
Select-Object Name, @{L="CreatedOn"; E={$_.BlobProperties.CreatedOn}} | `
Where-Object CreatedOn -gt (Get-Date).AddDays(-$daysAgo) | `
Sort-Object CreatedOn -Descending
IMPORTANT: You must be a Storage Account Blob Contributor on the account. I'm a subscription owner, but this didn't work on my account until I added this role. There may be other roles that work, but don't assume your subscription or resource group-level roles will work here.
Upvotes: 1