C#从Azure检索Blob列表

编程入门 行业动态 更新时间:2024-10-20 13:52:40
本文介绍了C#从Azure检索Blob列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一些存档清理代码,以便在特定保留期限后删除旧的Azure日志.

I need to have some archive cleanup code to remove old Azure logs after a certain retention period has occurred.

我知道我可以这样做:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(""); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("ctr"); var blobList = container.ListBlobs(); foreach(var blob in blobList) { logger.Info($"Blob Name: {blob.Uri}"); }

但是在我的容器中,结构是

However within my container the structure is

/ /year/month/day/hour/files

所以现在有

/2017/5/11/14/files /2017/5/11/17/files /2017/5/11/22/files /2017/5/11/23/files

/2017/5/12/11/files

其中文件是多个备份文件.

Where files is multiple backup files.

因为2017文件夹是根目录,所以for循环的集合中只有1个项目.

The for loop only has 1 item in it's collection as the 2017 folder is the root.

有没有办法检索所有斑点?

Is there a way to retrieve all blobs?

最终目标是删除所有早于保留期的斑点.

The end goal is to delete all blobs older than the retention period.

推荐答案

尝试此模式.在浏览大型存储时可以方便使用.我发现它对GC和内存占用空间更为友好

Try this pattern. Can be handy when browsing big storages. I found it much more GC and memory footprint friendly

var blobAccount = "<account>"; var apiKey = "<api-key>"; var containerName = "<container>"; var storageCredentials = new StorageCredentials(blobAccount, apiKey); var account = new CloudStorageAccount(storageCredentials, true); var blobClient = account.CreateCloudBlobClient(); var container = blobClient.GetContainerReference(containerName); var blobLimit = 500 if (container == null) { return; } var blobContinuationToken = new BlobContinuationToken(); using (var fs = new FileStream("Output.csv", FileMode.Create)) { var sw = new StreamWriter(fs); sw.WriteLine("Type,Name,Length"); BlobContinuationToken continuationToken = null; do { var blobList = container.ListBlobsSegmented("", true, BlobListingDetails.Metadata, blobLimit, continuationToken, new BlobRequestOptions { LocationMode = LocationMode.PrimaryOnly }, null); continuationToken = blobList.ContinuationToken; // I was looking only for BlockBlobs foreach (var item in blobList.Results.OfType<CloudBlockBlob>()) { sw.WriteLine($"block,\"{item.Name}\",{item.Properties.Length}"); } } while (continuationToken != null); }

更多推荐

C#从Azure检索Blob列表

本文发布于:2023-10-27 03:15:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532148.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   Azure   Blob

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!