Reputation: 115
good day, I am new to azure blob storage and experiencing issues in downloading image files from azure blob storage. Currently how I am doing it is getting the list of blobs file in pages and iterating over the pages to find the blob of current date and downloading it to local folder. I am doing all this in background service which runs every 5 minutes and downloads the files of current day. My storage has millions of files and every time my services executes the downloading method it starts from the beginning and wasting alot of time as there are millions of files in the storage. Please help me to get the only list of blob files which are added on current date. here is my code
BlobContainerClient blobContainerClient = new BlobContainerClient(_configuration.GetConnectionString("DefaultConnection"),
_configuration.GetValue<string>("Containers:Blobs"));
var blobslist = blobContainerClient.GetBlobsAsync().AsPages(continuationToken, 5000);
await foreach (Page<BlobItem> blobPage in blobslist)
{
try
{
var blobs = blobPage.Values.Where(x => x.Properties.CreatedOn.Value == DateTime.Now.Date).ToList();
if(blobs.Count() > 0)
{
foreach (BlobItem blobItem in blobs)
{
if (blobItem.Properties.CreatedOn != null && blobItem.Properties.CreatedOn.Value.Date == DateTime.Now.Date)
{
BlobClient blobClient = blobContainerClient.GetBlobClient(blobItem.Name);
var response = await blobClient.DownloadToAsync(@"Acacus_Images\Acacus\" + blobItem.Name);
}
}
}
continuationToken = blobPage.ContinuationToken;
}
catch (Exception ex)
{
}
}
Upvotes: 0
Views: 1693
Reputation: 58908
One way to do this more efficiently would be to upload each blob to a virtual folder with current date, so using file name like "2022-10-13/file.txt". Then you can filter by a prefix (REST API reference) to only list blobs that start with e.g. "2022-10-13/".
You can use the "prefix" parameter in blobContainerClient.GetBlobsAsync()
Upvotes: 0