简体   繁体   English

如何从 blob 位于子目录样式的容器中下载所有 blob

[英]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 文件,

  • container name = log-test容器名称 = 日志测试
  • Year folder年份文件夹
  • Month folder月文件夹
  • Date folder日期文件夹
  • Hour folder小时文件夹

在此处输入图片说明

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.

相关问题 Microsoft Azure:如何在 blob 容器中创建子目录 - Microsoft Azure: How to create sub directory in a blob container 从Azure容器和子目录获取所有Blob Uris - Get all blob Uris from Azure container and sub-directories 在 Azure Blob 容器中,如何按名称删除目录下的所有文件? - In Azure Blob container, How to delete all files by name under a directory? 如何使用C#从Azure容器/ Blob中读取/下载所有文件? - How to read/Download all files from a Azure container/Blob using C#? 使用 get 请求从 Blob Container 下载 Blob,不是预期的 output - Download blob from Blob Container with get request, not expected output 如何从blob下载文件? - How download file from blob? 如何从目录复制所有文件并将这些文件的副本移动到同一 azure blob 容器 c# 中的不同目录 - how to copy all files from a directory and move the copy of those file to different directory in same azure blob container c# 如何从Azure BLOB中的目录中获取所有文件 - How to get all files from a directory in Azure BLOB 如何使用 ListBlobsSegmentedAsync 从 Azure BLOB 中的目录中获取所有文件 - How to get all files from a directory in Azure BLOB using ListBlobsSegmentedAsync 如何获取具有子目录级别(n 级)的 Blob 容器中的所有 Blob? - How to get hold of all the blobs in a Blob container which has sub directories levels(n levels)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM