[英]how to download all blob from a container where blob sits in sub directory style
I have blob files which sits under container in a sub-directory style,我有位于子目录样式的容器下的 blob 文件,
Through C# background job every hour I need to downloads all the blob files for that Hour
folder.每小时通过 C# 后台作业,我需要下载该Hour
文件夹的所有 blob 文件。
Here in below code I am able to download one
file with hard code path (Year/Month/Date/Hour/blob-name), but how to download all the blob file for current date/time hour?在下面的代码中,我可以使用硬代码路径(年/月/日期/小时/blob 名称)下载one
文件,但是如何下载当前日期/时间小时的所有 blob 文件?
var blobServiceClient = new BlobServiceClient("conn-str");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("log-test");
BlobClient blobClient = containerClient.GetBlobClient("2021/08/11/01/log.txt");
await blobClient.DownloadToAsync(@"C:\Temp\Result.txt");
Note: - I tried this, but is this a performance issue?注意: - 我试过了,但这是性能问题吗? Can't I go directly to the actual HOUR folder?我不能直接转到实际的 HOUR 文件夹吗?
var blobItems = containerClient.GetBlobs().Where(blobItem => blobItem.Properties.LastModified != null
&& blobItem.Properties.LastModified.Value.Year == DateTime.UtcNow.Year
&& blobItem.Properties.LastModified.Value.Month == DateTime.UtcNow.Month
&& blobItem.Properties.LastModified.Value.Day == DateTime.UtcNow.Day
&& blobItem.Properties.LastModified.Value.Hour == DateTime.UtcNow.Hour)
.ToList();
foreach (var blob in blobItems)
{
BlobClient blobClient = containerClient.GetBlobClient(blob.Name);
await blobClient.DownloadToAsync($"C:\\Temp\\{blob.Name.Replace('/','-')}");
}
This code of yours你的这个代码
var blobItems = containerClient.GetBlobs().Where(blobItem => blobItem.Properties.LastModified != null
&& blobItem.Properties.LastModified.Value.Year == DateTime.UtcNow.Year
&& blobItem.Properties.LastModified.Value.Month == DateTime.UtcNow.Month
&& blobItem.Properties.LastModified.Value.Day == DateTime.UtcNow.Day
&& blobItem.Properties.LastModified.Value.Hour == DateTime.UtcNow.Hour)
.ToList();
is not optimized as it lists all blobs in the container and then do the filtering on the client side.未优化,因为它列出了容器中的所有 blob,然后在客户端进行过滤。
The method you would want to use is BlobContainerClient.GetBlobsByHierarchy
and specify Year/Month/Date/Hour/
as the value for the prefix
parameter.您要使用的方法是BlobContainerClient.GetBlobsByHierarchy
并指定Year/Month/Date/Hour/
作为prefix
参数的值。 You will then only get the blobs for that hour.然后,您将只获得那个小时的 blob。 Once you have that list, then you can download the blobs individually.获得该列表后,您可以单独下载 blob。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.